askIT

Android Equity calculator tutorial

applications-multimedia.png Video

01_app.jpg This tutorial will teach you how to create a application with two input fields and a button. It will use easy mathematics to calculate how much loan a user can have for house.

Make sure that you have downloaded and installed Android Studio and Java Development Kit on your computer.

The banks in Norway have a special rule for house mortages. If a customer wants to borrow 2 million, he need to have 300 000 in cash. We call this the 15 % rule.

Mortage formula
Mortage = (equity / 15) * 100

01 Create project

Start up Android Studio and select New Project.

02_nytt_prosjekt_welcome.jpg

Appliaction name = EquityCalculator
Company domain = yourname.com

Use API 15 (Android 4.0.3).This works on most mobile phones.

04_api_15.jpg

Select Empty Activity.

05_empty_activity.jpg

06_customize_the_activity_mainactivity.jpg

02 Design activity_main.xml

It is easiest to design application before writing any code. To start the designer go to.

app > res > layout > activity_main.xml

Drag en drop this items to your application.

Add 4 TextView.:
07_design_bmi_kalkulator.jpg

Add 2 Number (Decimal):
03_number_signed.jpg

Add 1 Button:
04_button.jpg

Change text of TextView:
You can change the text on a item by clicking on it. You can also change the apperance. Change the text for all TextView elements.

05_change_text.jpg

Change ID of Number (Signed), Button and TextView:
The code need to get the correct elemements. To make it easy to write the code you need to give elements IDs that are understandeble. Click on the Number Signed items, the Button and one of the TextView items. Give them a new ID in the "Properties" on the right side.

06_change_id.jpg

If you are having problems then you can copy the source code from the window below. Go to XML view by clicking on "Text".
12_text.jpg.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.nettport.equitycalculator.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:ems="10"
        android:id="@+id/editTextEquity"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="11dp" />

    <TextView
        android:text="Equity:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:layout_alignParentTop="true"
        android:layout_alignEnd="@+id/textView2" />

    <TextView
        android:text="Percentage:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="21dp"
        android:id="@+id/textView2"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:layout_below="@+id/editTextEquity"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="numberSigned"
        android:ems="10"
        android:layout_alignTop="@+id/textView2"
        android:layout_alignStart="@+id/editTextEquity"
        android:id="@+id/editTextPercentage" />

    <Button
        android:text="Calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editTextPercentage"
        android:layout_alignStart="@+id/editTextPercentage"
        android:layout_marginTop="10dp"
        android:id="@+id/buttonCalculate" />

    <TextView
        android:text="You can borrow:"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/buttonCalculate"
        android:layout_alignParentStart="true"
        android:layout_marginTop="37dp"
        android:id="@+id/textView3"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewOutput"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

You can start your application by clicking on Run.
11_kjor_app_i_emulator.jpg.

03 MainActivity

The code for your application is located in MainActivity.java

app > java > com.nettport.equitycalculator > MainActivity

We will start by making a method for calcualte. This method will be the code for calculation of how much loan the user can have

void calcualte(){
}

Try to run you app now to make sure it still works!

The next thing we need to do is create a on click listner for the Button. When the user clicks on the submit button then the calculate() method should run.

The onclick listner is going to be inside the onCreate method.

// Calculate button Onclick listner
Button buttonCalculate = (Button)findViewById(R.id.buttonCalculate);
buttonCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                // My code here
                calcualte();
        }
});

If you get error message "Cannot resolve symbol ..." then go to the error and click ALT+Enter. You need to import the libary.

Calculate method

We can now code the method calcualte(). We want to get variables from the design. After we have got the values we want to calculate and finally print the result on the screen of your mobile phone.

Notice: Undefined variable: encrypted in /customers/9/5/4/askit.one/httpd.www/en/android/basic/equity_calculator_tutorial.php on line 394 Notice: Undefined variable: file in /customers/9/5/4/askit.one/httpd.www/en/android/basic/equity_calculator_tutorial.php on line 394 Notice: Undefined variable: encrypted in /customers/9/5/4/askit.one/httpd.www/en/android/basic/equity_calculator_tutorial.php on line 394 Notice: Undefined variable: encrypted in /customers/9/5/4/askit.one/httpd.www/en/android/basic/equity_calculator_tutorial.php on line 395 Notice: Undefined variable: file in /customers/9/5/4/askit.one/httpd.www/en/android/basic/equity_calculator_tutorial.php on line 395 /* Calcualte */
void calcualte(){

        // Find variables
        EditText editTextEquity = (EditText)findViewById(R.id.editTextEquity);
        EditText editTextPercetage = (EditText)findViewById(R.id.editTextPercentage);
        TextView textViewOutput = (TextView)findViewById(R.id.textViewOutput);

        // Convert to string
        String stringEquity = editTextEquity.getText().toString();
        String stringPercentage = editTextPercetage.getText().toString();

        // Convert to double
        double ecuity = 0;
        ecuity = Double.parseDouble(stringEquity);

        double percentage = 0;
        percentage = Double.parseDouble(stringPercentage);

        // Calculation
        // Formula: Equity / Percetage * 100
        double answer = 0;
        answer = (ecuity/percentage) * 100;


        // Write number to output
        textViewOutput.setText("" + answer);

}

04 MainActivity code

If you didnt get the code to run then dont worry. You can look at the source code for MainActivity.java by following the link below.

Download source code

05 Start application

The application is finished and is ready to run in your emulator.

01_app.jpg

Now you can work with the design and new functionally.

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.