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 the "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 the "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 a 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 is 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. import android.os.Bundle;  
  3. import android.support.v7.app.AppCompatActivity;  
  4. import android.text.TextUtils;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.EditText;  
  8. public class MainActivity extends AppCompatActivity {  
  9.  Button emiCalcBtn;  
  10.  @Override  
  11.  protected void onCreate(Bundle savedInstanceState) {  
  12.   super.onCreate(savedInstanceState);  
  13.   setContentView(R.layout.activity_main);  
  14.   final EditText P = (EditText) findViewById(R.id.principal);  
  15.   final EditText I = (EditText) findViewById(R.id.interest);  
  16.   final EditText Y = (EditText) findViewById(R.id.years);  
  17.   final EditText TI = (EditText) findViewById(R.id.interest_total);  
  18.   final EditText result = (EditText) findViewById(R.id.emi);  
  19.   emiCalcBtn = (Button) findViewById(R.id.btn_calculate2);  
  20.   emiCalcBtn.setOnClickListener(new View.OnClickListener() {  
  21.    @Override  
  22.    public void onClick(View v) {  
  23.     String st1 = P.getText().toString();  
  24.     String st2 = I.getText().toString();  
  25.     String st3 = Y.getText().toString();  
  26.     if (TextUtils.isEmpty(st1)) {  
  27.      P.setError("Enter Prncipal Amount");  
  28.      P.requestFocus();  
  29.      return;  
  30.     }  
  31.     if (TextUtils.isEmpty(st2)) {  
  32.      I.setError("Enter Interest Rate");  
  33.      I.requestFocus();  
  34.      return;  
  35.     }  
  36.     if (TextUtils.isEmpty(st3)) {  
  37.      Y.setError("Enter Years");  
  38.      Y.requestFocus();  
  39.      return;  
  40.     }  
  41.     float p = Float.parseFloat(st1);  
  42.     float i = Float.parseFloat(st2);  
  43.     float y = Float.parseFloat(st3);  
  44.     float Principal = calPric(p);  
  45.     float Rate = calInt(i);  
  46.     float Months = calMonth(y);  
  47.     float Dvdnt = calDvdnt(Rate, Months);  
  48.     float FD = calFinalDvdnt(Principal, Rate, Dvdnt);  
  49.     float D = calDivider(Dvdnt);  
  50.     float emi = calEmi(FD, D);  
  51.     float TA = calTa(emi, Months);  
  52.     float ti = calTotalInt(TA, Principal);  
  53.     result.setText(String.valueOf(emi));  
  54.     TI.setText(String.valueOf(ti));  
  55.    }  
  56.   });  
  57.  }  
  58.  public float calPric(float p) {  
  59.   return (float)(p);  
  60.  }  
  61.  public float calInt(float i) {  
  62.   return (float)(i / 12 / 100);  
  63.  }  
  64.  public float calMonth(float y) {  
  65.   return (float)(y * 12);  
  66.  }  
  67.  public float calDvdnt(float Rate, float Months) {  
  68.   return (float)(Math.pow(1 + Rate, Months));  
  69.  }  
  70.  public float calFinalDvdnt(float Principal, float Rate, float Dvdnt) {  
  71.   return (float)(Principal * Rate * Dvdnt);  
  72.  }  
  73.  public float calDivider(float Dvdnt) {  
  74.   return (float)(Dvdnt - 1);  
  75.  }  
  76.  public float calEmi(float FD, Float D) {  
  77.   return (float)(FD / D);  
  78.  }  
  79.  public float calTa(float emi, Float Months) {  
  80.   return (float)(emi * Months);  
  81.  }  
  82.  public float calTotalInt(float TA, float Principal) {  
  83.   return (float)(TA - Principal);  
  84.  }  

Step 8
 
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
 
Android Studio
 
Step 9
 
Then, click the 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 a Simple EMI Calculator app for Android using Android Studio.
 
Android Studio


Similar Articles