Friday, 12 February 2016

Love Calculator - Android App


This is an android application which calculates the love percentage between two people.

The logic is it converts the letters in the name to its ASCII equivalent and then sums up all the letters and perform a modulus operation with 100 to get the result.



Example  :  If 2 names are Kete and Rade
                    ASCII value of K   =     75
                    ASCII value of E    =     69
                    ASCII value of T    =     84
                    ASCII value of E    =     69
                    ASCII value of R    =     72
                    ASCII value of A    =     65
                    ASCII value of D    =     68
                    ASCII value of E    =     69

                    Sum of  ASCII values of K+E+T+E + R + A + D + E =     581
                    now 581 % 100 = 81

                  So, the love % between Kete and  Rade= 81%


Here we use two edit texts for entering name and crush name   , one button for calculating , and one textview for showing result.

In this app we use material design , as material design is not compatable with pre lolipop devices , we use a design support library

add these two lines in build.gradle file :
dependencies {
   ....
compile 'com.android.support:appcompat-v7:23.0.1'compile 'com.android.support:design:23.0.1'}

Get the .apk file    Application on Github

For the edit text labels we use , floating labels which is from the material design , so in your layout xml file , place this code ,
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textColorHint="@android:color/white">

<EditText
android:id="@+id/nameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:ems="10"
android:gravity="center"
android:hint="Enter your name"
android:inputType="textPersonName"
android:textColor="#FFF"
android:textColorHint="@android:color/white"
android:textSize="25sp"
/>
</android.support.design.widget.TextInputLayout>
similarly for the second edit text by just changing the label name to "Enter your crush name"

For button and textview , place this code ,
<Button
android:id="@+id/resbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@android:color/transparent"
android:text="Calculate"
android:textColor="@android:color/white"
android:textSize="20sp" />

<TextView
android:id="@+id/resview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textSize="20sp" />

Get the .apk file    Application on Github
Now , in the main activity , initialize these two edit texts , button for calculating, and textview for showing result,
Button cal = (Button) findViewById(R.id.resbtn);
final TextView res = (TextView) findViewById(R.id.resview);
final EditText name = (EditText) findViewById(R.id.nameField);
final EditText cname = (EditText) findViewById(R.id.crushnameField);
Now when the calculate button is clicked ,
first, we get the text from them
next , convert them to strings and concat them ,
next , convert the letters to ASCII and the sum all the letters
next , perform modulus operation with 100 on that sum and show the result.
cal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                res.setText("");
                Editable n = name.getText();
                Editable cn = cname.getText();
                String concat = String.valueOf(n).concat(String.valueOf(cn)).toUpperCase();
                if ((n.toString().trim().length() == 0) || (cn.toString().trim().length() == 0)) {
                    Toast.makeText(MainActivity.this, "Please fill both the fields ", Toast.LENGTH_LONG).show();
                } else {
                    int sum = 0;
                    for (int i = 0; i < concat.length(); i++) {
                        char character = concat.charAt(i);
                        int ascii = (int) character;
                        sum += ascii;
                    }
                    res.setText("The love between " + n + " and " + cn + " is " + sum % 100 + "%");
                }
            }
        });
Get the .apk file    Application on Github

OUTPUT :

Android - Love Calculator 
Android - Love Calculator