How to develop simple EMI calculator Android App

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:

where

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

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;

publicclassMainActivityextendsAppCompatActivity{

Button emiCalcBtn;

@Override

protectedvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

finalEditTextP=(EditText)findViewById(R.id.principal);

finalEditTextI=(EditText)findViewById(R.id.interest);

finalEditTextY=(EditText)findViewById(R.id.years);

finalEditText TI=(EditText)findViewById(R.id.interest_total);

finalEditText result=(EditText)findViewById(R.id.emi);

emiCalcBtn=(Button)findViewById(R.id.btn_calculate2);

emiCalcBtn.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

Stringst1=P.getText().toString();

Stringst2=I.getText().toString();

Stringst3=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;

}

floatp=Float.parseFloat(st1);

floati=Float.parseFloat(st2);

floaty=Float.parseFloat(st3);

floatPrincipal=calPric(p);

floatRate=calInt(i);

floatMonths=calMonth(y);

floatDvdnt=calDvdnt(Rate,Months);

floatFD=calFinalDvdnt(Principal,Rate,Dvdnt);

floatD=calDivider(Dvdnt);

floatemi=calEmi(FD,D);

floatTA=calTa(emi,Months);

floatti=calTotalInt(TA,Principal);

result.setText(String.valueOf(emi));

TI.setText(String.valueOf(ti));

}

});

}

publicfloatcalPric(floatp){

return(float)(p);

}

publicfloatcalInt(floati){

return(float)(i/12/100);

}

publicfloatcalMonth(floaty){

return(float)(y *12);

}

publicfloatcalDvdnt(floatRate,floatMonths){

return(float)(Math.pow(1+Rate,Months));

}

publicfloatcalFinalDvdnt(floatPrincipal,floatRate,floatDvdnt){

return(float)(Principal *Rate *Dvdnt);

}

publicfloatcalDivider(floatDvdnt){

return(float)(Dvdnt-1);

}

publicfloatcalEmi(floatFD,FloatD){

return(float)(FD/D);

}

publicfloatcalTa(floatemi,FloatMonths){

return(float)(emi*Months);

}

publicfloatcalTotalInt(floatTA,floatPrincipal){

return(float)(TA-Principal);

}

}

Next open activity_main.xml and remove all code and paste below code into it.

activity_main.xml

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

<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

1

2

3

4

5

<stringname="hint_principal">Principal Amount₹</string>

<stringname="hint_interest">Interest rate per Year%</string>

<stringname="hint_years">How Many Years</string>

<stringname="hint_emi">EMI₹</string>

<stringname="hint_interest_total">Total Interest forLoan₹</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$