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,
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. <android.support.design.widget.TextInputLayout
  21. android:id="@+id/input_layout_principal"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content">
  24. <EditText
  25. android:id="@+id/principal"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. android:singleLine="true"
  29. android:inputType="number"
  30. android:digits="0123456789."
  31. android:hint="@string/hint_principal" />
  32. </android.support.design.widget.TextInputLayout>
  33. <android.support.design.widget.TextInputLayout
  34. android:id="@+id/input_layout_interest"
  35. android:layout_width="match_parent"
  36. android:layout_height="wrap_content">
  37. <EditText
  38. android:id="@+id/interest"
  39. android:layout_width="match_parent"
  40. android:layout_height="wrap_content"
  41. android:singleLine="true"
  42. android:inputType="number"
  43. android:digits="0123456789."
  44. android:hint="@string/hint_interest" />
  45. </android.support.design.widget.TextInputLayout>
  46. <android.support.design.widget.TextInputLayout
  47. android:id="@+id/input_layout_tenure"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content">
  50. <EditText
  51. android:id="@+id/years"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:inputType="number"
  55. android:digits="0123456789."
  56. android:hint="@string/hint_years" />
  57. </android.support.design.widget.TextInputLayout>
  58. <Button android:id="@+id/btn_calculate2"
  59. android:layout_width="fill_parent"
  60. android:layout_height="wrap_content"
  61. android:text="Calculate"
  62. android:background="@color/colorPrimary"
  63. android:layout_marginTop="40dp"
  64. android:textColor="@android:color/white"/>
  65. <android.support.design.widget.TextInputLayout
  66. android:id="@+id/input_layout_emi"
  67. android:layout_width="match_parent"
  68. android:layout_height="wrap_content"
  69. android:layout_marginTop="40dp">
  70. <EditText
  71. android:id="@+id/emi"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. android:maxEms="0"
  75. android:inputType="number"
  76. android:hint="@string/hint_emi" />
  77. </android.support.design.widget.TextInputLayout>
  78. <android.support.design.widget.TextInputLayout
  79. android:id="@+id/input_layout_total_Interest"
  80. android:layout_width="match_parent"
  81. android:layout_height="wrap_content"
  82. android:layout_marginTop="10dp">
  83. <EditText
  84. android:id="@+id/interest_total"
  85. android:layout_width="match_parent"
  86. android:layout_height="wrap_content"
  87. android:inputType="number"
  88. android:hint="@string/hint_interest_total" />
  89. </android.support.design.widget.TextInputLayout>
  90. </LinearLayout>
  91. </android.support.v4.widget.NestedScrollView>
  92. </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. }
  85. }
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