How To Develop A Simple EMI Calculator Using Android Studio

Introduction

Android is one of the most popular operating systems for mobile. In this article, I will show you how to create a Simple EMI Calculator Android application using Android Studio.

Requirements

Steps to be followed

 These steps are required to create a simple EMI Calculator Android application using Android Studio and I have included the source code below.

Step 1

Open Android Studio and Start a New Android Studio Project.

Android Studio 

Step 2

You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click "Next" button.

Android Studio

Now, select the version of Android and select the target Android devices.

Android Studio 

Step 3

Now, add the activity and click "Next" button.

Android Studio

Add Activity name and click "Finish".

Android Studio 

Formula

Here’s the formula to calculate EMI,

Android Studio
where,

  • E is EMI
  • P is Principal Loan Amount
  • r is the rate of interest calculated on monthly basis.
  • n is loan term/tenure / duration in number of months

Step 4

Go to activity_main.xml. This XML file contains the designing code for an Android app in the activity_main.xml,

Android Studio

The XML code given below.

  1. <android.support.design.widget.CoordinatorLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  6.     tools:context="abu.emicalculator.MainActivity"  
  7.     android:layout_height="match_parent">  
  8.     <android.support.v4.widget.NestedScrollView  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         app:layout_behavior="@string/appbar_scrolling_view_behavior">  
  12.         <LinearLayout  
  13.             android:layout_width="fill_parent"  
  14.             android:layout_height="match_parent"  
  15.             android:layout_marginTop="?attr/actionBarSize"  
  16.             android:orientation="vertical"  
  17.             android:paddingLeft="20dp"  
  18.             android:paddingRight="20dp"  
  19.             android:paddingTop="10dp">  
  20.   
  21.             <android.support.design.widget.TextInputLayout  
  22.                 android:id="@+id/input_layout_principal"  
  23.                 android:layout_width="match_parent"  
  24.                 android:layout_height="wrap_content">  
  25.   
  26.                 <EditText  
  27.                     android:id="@+id/principal"  
  28.                     android:layout_width="match_parent"  
  29.                     android:layout_height="wrap_content"  
  30.                     android:singleLine="true"  
  31.                     android:inputType="number"  
  32.                     android:digits="0123456789."  
  33.                     android:hint="@string/hint_principal" />  
  34.   
  35.             </android.support.design.widget.TextInputLayout>  
  36.   
  37.   
  38.             <android.support.design.widget.TextInputLayout  
  39.                 android:id="@+id/input_layout_interest"  
  40.                 android:layout_width="match_parent"  
  41.                 android:layout_height="wrap_content">  
  42.   
  43.                 <EditText  
  44.                     android:id="@+id/interest"  
  45.                     android:layout_width="match_parent"  
  46.                     android:layout_height="wrap_content"  
  47.                     android:singleLine="true"  
  48.                     android:inputType="number"  
  49.                     android:digits="0123456789."  
  50.                     android:hint="@string/hint_interest" />  
  51.   
  52.             </android.support.design.widget.TextInputLayout>  
  53.   
  54.             <android.support.design.widget.TextInputLayout  
  55.                 android:id="@+id/input_layout_tenure"  
  56.                 android:layout_width="match_parent"  
  57.                 android:layout_height="wrap_content">  
  58.   
  59.                 <EditText  
  60.                     android:id="@+id/years"  
  61.                     android:layout_width="match_parent"  
  62.                     android:layout_height="wrap_content"  
  63.                     android:inputType="number"  
  64.                     android:digits="0123456789."  
  65.                     android:hint="@string/hint_years" />  
  66.             </android.support.design.widget.TextInputLayout>  
  67.   
  68.   
  69.             <Button android:id="@+id/btn_calculate2"  
  70.                 android:layout_width="fill_parent"  
  71.                 android:layout_height="wrap_content"  
  72.                 android:text="Calculate"  
  73.                 android:background="@color/colorPrimary"  
  74.                 android:layout_marginTop="40dp"  
  75.                 android:textColor="@android:color/white"/>  
  76.   
  77.   
  78.             <android.support.design.widget.TextInputLayout  
  79.                 android:id="@+id/input_layout_emi"  
  80.                 android:layout_width="match_parent"  
  81.                 android:layout_height="wrap_content"  
  82.                 android:layout_marginTop="40dp">  
  83.   
  84.                 <EditText  
  85.                     android:id="@+id/emi"  
  86.                     android:layout_width="match_parent"  
  87.                     android:layout_height="wrap_content"  
  88.                     android:maxEms="0"  
  89.                     android:inputType="number"  
  90.                     android:hint="@string/hint_emi" />  
  91.             </android.support.design.widget.TextInputLayout>  
  92.   
  93.             <android.support.design.widget.TextInputLayout  
  94.                 android:id="@+id/input_layout_total_Interest"  
  95.                 android:layout_width="match_parent"  
  96.                 android:layout_height="wrap_content"  
  97.                 android:layout_marginTop="10dp">  
  98.   
  99.                 <EditText  
  100.                     android:id="@+id/interest_total"  
  101.                     android:layout_width="match_parent"  
  102.                     android:layout_height="wrap_content"  
  103.   
  104.                     android:inputType="number"  
  105.                     android:hint="@string/hint_interest_total" />  
  106.             </android.support.design.widget.TextInputLayout>  
  107.   
  108.         </LinearLayout>  
  109.     </android.support.v4.widget.NestedScrollView>  
  110.   
  111. </android.support.design.widget.CoordinatorLayout>  

Step 5

Go to (gradle scripts ⇒ build.gradle(moduleapp). And, add the below code.

Android Studio

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.1.0' 

Step 6

Go to (App ⇒ Res ⇒values⇒String.xml).

Android Studio

The string XML Code Given Below

  1. <resources>  
  2.     <string name="app_name">EMI Calculator</string>  
  3.     <string name="hint_principal">Principal Amount ₹</string>  
  4.     <string name="hint_interest">Interest rate per Year %</string>  
  5.     <string name="hint_years">How Many Years</string>  
  6.     <string name="hint_emi">EMI ₹</string>  
  7.     <string name="hint_interest_total">Total Interest for Loan ₹</string>  
  8. </resources>   

Step 7

Go to Main Activity.java. This Java program is the back-end language for Android. The Java code is given below.

  1. package abu.emicalculator;  
  2.   
  3. import android.os.Bundle;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.text.TextUtils;  
  6. import android.view.View;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9.   
  10.   
  11. public class MainActivity extends AppCompatActivity {  
  12.   
  13.     Button emiCalcBtn;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         final EditText P = (EditText)findViewById(R.id.principal);  
  21.         final EditText I = (EditText)findViewById(R.id.interest);  
  22.         final EditText Y = (EditText)findViewById(R.id.years);  
  23.         final EditText TI = (EditText)findViewById(R.id.interest_total);  
  24.   
  25.   
  26.   
  27.   
  28.         final EditText result = (EditText)findViewById(R.id.emi) ;  
  29.   
  30.   
  31.         emiCalcBtn = (Button) findViewById(R.id.btn_calculate2);  
  32.   
  33.         emiCalcBtn.setOnClickListener(new View.OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View v) {  
  36.   
  37.                 String st1 = P.getText().toString();  
  38.                 String st2 = I.getText().toString();  
  39.                 String st3 = Y.getText().toString();  
  40.   
  41.                 if (TextUtils.isEmpty(st1)) {  
  42.                     P.setError("Enter Prncipal Amount");  
  43.                     P.requestFocus();  
  44.                     return;  
  45.                 }  
  46.   
  47.                 if (TextUtils.isEmpty(st2)) {  
  48.                     I.setError("Enter Interest Rate");  
  49.                     I.requestFocus();  
  50.                     return;  
  51.                 }  
  52.   
  53.                 if (TextUtils.isEmpty(st3)) {  
  54.                     Y.setError("Enter Years");  
  55.                     Y.requestFocus();  
  56.                     return;  
  57.                 }  
  58.   
  59.                 float p = Float.parseFloat(st1);  
  60.                 float i = Float.parseFloat(st2);  
  61.                 float y = Float.parseFloat(st3);  
  62.   
  63.                 float Principal = calPric(p);  
  64.   
  65.                 float Rate = calInt(i);  
  66.   
  67.                 float Months = calMonth(y);  
  68.   
  69.                 float Dvdnt = calDvdnt( Rate, Months);  
  70.   
  71.                 float FD = calFinalDvdnt (Principal, Rate, Dvdnt);  
  72.   
  73.                 float D = calDivider(Dvdnt);  
  74.   
  75.                 float emi = calEmi(FD, D);  
  76.   
  77.                 float TA = calTa (emi, Months);  
  78.   
  79.                 float ti = calTotalInt(TA, Principal);  
  80.   
  81.   
  82.   
  83.                 result.setText(String.valueOf(emi));  
  84.   
  85.   
  86.   
  87.   
  88.                 TI.setText(String.valueOf(ti));  
  89.   
  90.             }  
  91.         });  
  92.     }  
  93.   
  94.     public  float calPric(float p) {  
  95.   
  96.         return (float) (p);  
  97.   
  98.     }  
  99.   
  100.     public  float calInt(float i) {  
  101.   
  102.         return (float) (i/12/100);  
  103.   
  104.     }  
  105.   
  106.     public  float calMonth(float y) {  
  107.   
  108.         return (float) (y * 12);  
  109.   
  110.     }  
  111.   
  112.     public  float calDvdnt(float Rate, float Months) {  
  113.   
  114.         return (float) (Math.pow(1+Rate, Months));  
  115.   
  116.     }  
  117.   
  118.     public  float calFinalDvdnt(float Principal, float Rate, float Dvdnt) {  
  119.   
  120.         return (float) (Principal * Rate * Dvdnt);  
  121.   
  122.     }  
  123.   
  124.     public  float calDivider(float Dvdnt) {  
  125.   
  126.         return (float) (Dvdnt-1);  
  127.   
  128.     }  
  129.   
  130.     public  float calEmi(float FD, Float D) {  
  131.   
  132.         return (float) (FD/D);  
  133.   
  134.     }  
  135.   
  136.     public  float calTa(float emi, Float Months) {  
  137.   
  138.         return (float) (emi*Months);  
  139.   
  140.     }  
  141.   
  142.     public  float calTotalInt(float TA, float Principal) {  
  143.   
  144.         return (float) (TA - Principal);  
  145.   
  146.     }  
  147. }   

Step 8

Now, either go to menu bar and click "Make project" or press ctrl+f9 to debug the error.

Android Studio

Step 9

Then, click Run button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.

Android Studio

Conclusion

We have successfully created Simple EMI Calculator app for Android using Android Studio.

Android Studio

Up Next
    Ebook Download
    View all
    Learn
    View all