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.


Appliaction name = EquityCalculator
Company domain =

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


Select Empty Activity.



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.:

Add 2 Number (Decimal):

Add 1 Button:

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.


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.


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


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:layout_marginEnd="11dp" />

        android:layout_alignEnd="@+id/textView2" />

        android:layout_alignParentStart="true" />

        android:id="@+id/editTextPercentage" />

        android:id="@+id/buttonCalculate" />

        android:text="You can borrow:"
        android:textAppearance="@style/TextAppearance.AppCompat.Medium" />

        android:layout_centerHorizontal="true" />

You can start your application by clicking on Run.

03 MainActivity

The code for your application is located in

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(;
buttonCalculate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
                // My code here

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/ on line 394 Notice: Undefined variable: file in /customers/9/5/4/ on line 394 Notice: Undefined variable: encrypted in /customers/9/5/4/ on line 394 Notice: Undefined variable: encrypted in /customers/9/5/4/ on line 395 Notice: Undefined variable: file in /customers/9/5/4/ on line 395 /* Calcualte */
void calcualte(){

        // Find variables
        EditText editTextEquity = (EditText)findViewById(;
        EditText editTextPercetage = (EditText)findViewById(;
        TextView textViewOutput = (TextView)findViewById(;

        // 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 by following the link below.

Download source code

05 Start application

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


Now you can work with the design and new functionally.

Add comment






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