EMI_Calculator

In this Post I will teach you how to develop Simple EMI Calculator Android App in Android Studio. By this you will also learn about how to design a layout in XML, what is EditText, how to get user input from EditText, how to use methods, how to use Android Click listener etc.,

Before starting the procedure we need to know Formula for EMI Calculation.

 

Here’s the formula to calculate EMI:

EMI Calculatorwhere

  • E is EMI
  • P is Principal Loan Amount
  • r is rate of interest calculated on monthly basis. (i.e., r = Rate of Annual interest/12/100. If rate of interest is 10.5% per year, then r = 10.5/12/100=0.00875)
  • n is loan term / tenure / duration in number of months

Note: Watch Video below for better understanding the code.

https://youtu.be/CBoR8dbFWKg

Procedure:

  • Create a new Android Studio project with name Simple EMI Calculator with blank activity.
  • After project load completely navigate to MainActivity.java and remove whole code except first line and paste below code after the first line.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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);

    }
}
  • Next open activity_main.xml and remove all code and paste below code into it.
<android.support.design.widget.CoordinatorLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.androidmkab.emicalculator.MainActivity"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="10dp">

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_principal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/principal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"
android:digits="0123456789."
android:hint="@string/hint_principal" />

</android.support.design.widget.TextInputLayout>


<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_interest"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/interest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:inputType="number"
android:digits="0123456789."
android:hint="@string/hint_interest" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_tenure"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/years"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="0123456789."
android:hint="@string/hint_years" />
</android.support.design.widget.TextInputLayout>


<Button android:id="@+id/btn_calculate2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Calculate"
android:background="@color/colorPrimary"
android:layout_marginTop="40dp"
android:textColor="@android:color/white"/>


<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_emi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp">

<EditText
android:id="@+id/emi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxEms="0"
android:inputType="number"
android:hint="@string/hint_emi" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/input_layout_total_Interest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">

<EditText
android:id="@+id/interest_total"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:inputType="number"
android:hint="@string/hint_interest_total" />
</android.support.design.widget.TextInputLayout>

</LinearLayout>
</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>
  • Now add below lines to your strings.xml
<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>

Now Run the EMI Calculator app in Emulator and check it.

  • You can check my Easy Calculator app in PlayStore If you want template of my app easy calculator which has EMI Calculator, Discount calculator and percentage calculator you can buy it for 1$Easy Calculator Template

[wp_cart:Easy Calculator Template:price:1.00:end]