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.
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.
Now, select the version of Android and select the target Android devices.
Step 3
Now, add the activity and click the "Next" button.
Add Activity name and click "Finish".
Formula
Here’s the formula to calculate EMI,
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,
The XML code is given below.
Step 5
Go to (gradle scripts ⇒ build.gradle(moduleapp). And, add the below code.
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).
The string XML Code Given Below
- <resources>
- <string name="app_name">EMI Calculator</string>
- <string name="hint_principal">Principal Amount ₹</string>
- <string name="hint_interest">Interest rate per Year %</string>
- <string name="hint_years">How Many Years</string>
- <string name="hint_emi">EMI ₹</string>
- <string name="hint_interest_total">Total Interest for Loan ₹</string>
- </resources>
Step 7
Go to Main Activity.java. This Java program is the back-end language for Android. The Java code is given below.
- package abu.emicalculator;
- import android.os.Bundle;
- import android.support.v7.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends AppCompatActivity {
- Button emiCalcBtn;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final EditText P = (EditText) findViewById(R.id.principal);
- final EditText I = (EditText) findViewById(R.id.interest);
- final EditText Y = (EditText) findViewById(R.id.years);
- final EditText TI = (EditText) findViewById(R.id.interest_total);
- final EditText result = (EditText) findViewById(R.id.emi);
- emiCalcBtn = (Button) findViewById(R.id.btn_calculate2);
- emiCalcBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String st1 = P.getText().toString();
- String st2 = I.getText().toString();
- String st3 = Y.getText().toString();
- if (TextUtils.isEmpty(st1)) {
- P.setError("Enter Prncipal Amount");
- P.requestFocus();
- return;
- }
- if (TextUtils.isEmpty(st2)) {
- I.setError("Enter Interest Rate");
- I.requestFocus();
- return;
- }
- if (TextUtils.isEmpty(st3)) {
- Y.setError("Enter Years");
- Y.requestFocus();
- return;
- }
- float p = Float.parseFloat(st1);
- float i = Float.parseFloat(st2);
- float y = Float.parseFloat(st3);
- float Principal = calPric(p);
- float Rate = calInt(i);
- float Months = calMonth(y);
- float Dvdnt = calDvdnt(Rate, Months);
- float FD = calFinalDvdnt(Principal, Rate, Dvdnt);
- float D = calDivider(Dvdnt);
- float emi = calEmi(FD, D);
- float TA = calTa(emi, Months);
- float ti = calTotalInt(TA, Principal);
- result.setText(String.valueOf(emi));
- TI.setText(String.valueOf(ti));
- }
- });
- }
- public float calPric(float p) {
- return (float)(p);
- }
- public float calInt(float i) {
- return (float)(i / 12 / 100);
- }
- public float calMonth(float y) {
- return (float)(y * 12);
- }
- public float calDvdnt(float Rate, float Months) {
- return (float)(Math.pow(1 + Rate, Months));
- }
- public float calFinalDvdnt(float Principal, float Rate, float Dvdnt) {
- return (float)(Principal * Rate * Dvdnt);
- }
- public float calDivider(float Dvdnt) {
- return (float)(Dvdnt - 1);
- }
- public float calEmi(float FD, Float D) {
- return (float)(FD / D);
- }
- public float calTa(float emi, Float Months) {
- return (float)(emi * Months);
- }
- public float calTotalInt(float TA, float Principal) {
- return (float)(TA - Principal);
- }
- }
Step 8
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
Step 9
Then, click the Run button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.
Conclusion
We have successfully created a Simple EMI Calculator app for Android using Android Studio.