Home   Courses   Blog   About

Make A Simple Android Love Calculator App

I wanted to make a Love Calculator App for a long time. I remembered how much fun we had in school writing down names and calculating percentage of love for each other. It was pure fun. We learned a little bit less for a few hours but at least we were programming :D This will be an Android App. Well I know now that this is a algorithm, but back then it was just fun. Since we have already learned what the function is in our blog What is function and how to get color for your name this is the next step, since we are building an algorithm for calculating love percentage.

I have recorded a video on how to calculate love percentage on paper. Because I want you to try it on paper. This way you will see how the algorithm works. In the future there won't be a lot of algorithms you can use on paper at least not on a small piece of paper.

Here is the algorithm in words

I will use “Mary" and "James" so the combined text is "Mary Loves James”

1. Count all the letters from left to right and write down the number
- First one is “M” and it appears twice, so we write down 2
- Next is “A” and appears twice, write down 2
- Don’t repeat letters (on paper just strike the ones you have counted) and continue to the end
- You should get 2211111221

2. Second part is getting a shorter number
- You will do this by adding leftmost and rightmost numbers
- So first will be 2 + 1 and write down this number, next 2 + 2, 1 + 2…, continue this till you reach the middle, if only one number is left just write it down
    - Again you should strike the numbers you add together
- You should get 34322

3. Next step is repeating step 2 until you get only two numbers
- For our example this is two more times
- And you should get 563 for the first repeat
- And the result should be 86

4. So the result is 86%

They are just deeply in love if I might say :D Try it on paper for you and your secret lover, remember to hide names if you don’t want othera to find out :D

The algorithm is just great for paper. We will have little work to do, for it to work in Java. But we can do it. We will do it in steps as on paper. But first we will have to prepare a screen to get all the data.

Let’s start programming


1. Create a new project

Take a look at our blog on How to create a new project

2. Position elements on screen

We will use ConstraintLayout to position all the elements on the screen. So be careful to make all the necessary connections between controls. Check with the picture below to see the final layout.

Add two EditText-s (in Palete you will find the PlainText control)
- name the first control “textName1” and the second control “textName2”.
   - make sure, that textName1 has 3 constraints one to the top of the screen, one to the left side of the screen and one to the right
   - same goes for textName2 where the top constraint should be linked to textName1
- Set hint to “Enter her name” for first EditText and “Enter his name” for second EditText

Add one TextView
- Name it “textViewResult”
- Set text to empty text (“Press calculate”)
- make sure, that it has 3 constraints one to the top linked to textName2, one to the left side of the screen and one to the right

Add one Button on screen
- name it “buttonCalculate” and
- change the text to “Calculate”
- button should also have 3 constraints one to the bottom, one to the left and last to the right side of the screen

The final layout should look like this

Final Layout Love Calculator

3. Go to MainActivity.java and prepare all variables to work with controls, these code should go after the MainActivity class definition.


4. Once we have variables we can initialize values for them. We will do this in the onCreate function of the MainActivity

Find controls

5. We will also add a onClickListener for buttonCalculate


6. We will now get the text from EditText fields, so we will read the names you have entered.

Get Names from EditTexts

We will need values we have read to calculate the love percentage. Add this two lines just after OnClick… (after { of course). The whole code is visible after counting chars function bellow.

Now we will add functions that will calculate your love

Counting chars

1. I will show you the calculation in 3 steps. Similar to the algorithm I have showed on paper. To get the calculation in the first step we will count the characters in names and in the word “loves”. We will do this in the function countChars. I will go trough this function step by step and explain what is happening.


Explanation: This function takes 2 parameters and adds “loves” in the middle to get the combined string. Than it goes character by character to the end of the combinedString and counts the occurrences of that character. It does so with two “for each” loops on for getting character and the other for counting. Once it counts occurrences it stores character and leave it out for the next time so that it will not repeat.

Since we will do this step by step you will add a code to the button onClickListener and try it out. You will have to add the second two lines, the first will execute the function for counting characters and the second will show the result on screen.

OnClickListener first

Now lets run it for the first time. If you enter Mary and James you should see the result like this.

Screen shot just counting chars

And the result should be the same as we calculated on paper 2211111221.

This is the first step.


We will now write a function to shorten the number

It looks like this:

shortenNumber function

This function uses recursion to calculate a result, this means, that the function calls itself inside, you will learn how to write this kind of functions, but for now just follow the instructions. The function is divided in two parts according to length (with if statement), if we have at least two chars we will sum numbers on the edge, otherwise we will just return the string.

And if we use this function in our onClickListener it should look like this. This is a different implementation of an onClickListener, to show shortening of a number.

OnClickListener second

If you run it for Mary and James, it will look like this on screen:

Screenshot Shorten Number

Making the Algorithm Work

We have come to the last step and it is making the whole algorithm work. We will do this with the calculate function.

calculate function

What this function does is it repeats steps we have done so far to calculate a number, that is only two numbers long. We do this with do while loop. It will run for the first time and check if it runs again in the end, for our case (shortString.length() > 2). In the end of each loop it will append shortString or number for this line to otput string. This a string (output) that is meant to show result in nicer way with new lines ("\n") and percentage in the end.

The onClickListner will look like this

OnClickListener Final

Final result for our love couple is on screen with nice score of 86%. Check it out, try with first name, last name, combination...

Love Calculator Working Algorithm Result