Saturday, May 23, 2015

Simple EMI Loan calculator android example

In this example we learn, how to write simple EMI Loan Calculator. in this example user can calculate loan amount as per monthly installment (e.g pay monthly 1000rs.) or monthly time period (e.g.10 month)with interest rate.

CREATE NEW PROJECT


Step 1:write code into activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

        <LinearLayout

            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <EditText

                android:id="@+id/etLoanAmount"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="   LoanAmount"
                android:inputType="numberDecimal"
                android:textColor="#fff"
                android:textSize="25sp" />

            <EditText

                android:id="@+id/etInterest"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="   Annuan Interest Rate"
                android:inputType="numberDecimal"
                android:textColor="#fff"
                android:textSize="25sp" />

            <Spinner

                android:id="@+id/sp1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:spinnerMode="dialog" />

            <EditText

                android:id="@+id/e1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="Enter value"
                android:inputType="numberDecimal"
                android:textColor="#fff"
                android:textSize="25sp" />

            <Button

                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Calculate"
                android:textColor="#fff"
                android:textSize="20dp" />

            <TextView

                android:id="@+id/etAns"
                android:layout_width="match_parent"
                android:layout_height="38dp"
                android:ems="10"
                android:hint="  Answer"
                android:textColor="#fff"
                android:textSize="25sp" >

                <requestFocus />

            </TextView>
       </LinearLayout>
</LinearLayout>

Step 2:write code into spineritem.xml its custom spiner, you can create your own style.e.g change background color or font color etc.



<TextView xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:textStyle="italic"
    android:fadingEdgeLength="20dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="15dp"
    android:textColor="#0594A8"
    android:textSize="25sp" >


</TextView>

Step 3:write code into MainActivity.java


package com.loancalculator.calLoan;


import java.util.ArrayList;

import java.util.List;

import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


Spinner sp1;

Button button1, bthlp;
Float f1, f2, f3, f4, f5, f6;
EditText etLoanAmount, etInterest, etAns, e1;
TextView tvans;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = (Spinner) findViewById(R.id.sp1);
button1 = (Button) findViewById(R.id.button1);
etLoanAmount = (EditText) findViewById(R.id.etLoanAmount);
etInterest = (EditText) findViewById(R.id.etInterest);
e1 = (EditText) findViewById(R.id.e1);

tvans = (TextView) findViewById(R.id.etAns);


List<String> list = new ArrayList<String>();

list.add("Select a type");
list.add("Monthly time Period");
list.add("Installment price");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.spineritem, list);
sp1.setAdapter(dataAdapter);

sp1.setOnItemSelectedListener(new OnItemSelectedListener() {


@Override

public void onItemSelected(AdapterView<?> parent, View arg1,
int pos, long id) {
// TODO Auto-generated method stub

String s = parent.getItemAtPosition(pos).toString();


if (s.equals("Monthly time Period"))

{
button1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {
// TODO Auto-generated method stub


String a = etLoanAmount.getText().toString();
String m = etInterest.getText().toString();
String d = e1.getText().toString();

if(a.length()<=0 || m.length()<=0 ||d.length()<=0)
{
Toast.makeText(getApplicationContext(), "Empty Text Field Enter Data ", Toast.LENGTH_LONG).show();
}
else
{
f1 = Float.parseFloat(etLoanAmount.getText()
.toString());

try {


f2 = Float.parseFloat(etInterest.getText()

.toString());
} catch (NumberFormatException e) {
// TODO: handle exception
e.printStackTrace();
}

f5 = Float.parseFloat(e1.getText().toString());


f3 = (f1 * f2 / 100);

f4 = f3 + f1;
f6 = f4 / f5;
String s2 = String.valueOf(f6);
tvans.setText(s2 + " Rs Installment");


}

}
});
}
if (s.equals("Installment price")) {
button1.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {
// TODO Auto-generated method stub


String a = etLoanAmount.getText().toString();
String m = etInterest.getText().toString();
String d = e1.getText().toString();

if(a.length()<=0 || m.length()<=0 ||d.length()<=0)

{
Toast.makeText(getApplicationContext(), "Empty Text Field Enter Data ", Toast.LENGTH_LONG).show();
}

else{
f1 = Float.parseFloat(etLoanAmount.getText()
.toString());
try {
f2 = Float.valueOf((etInterest.getText()
.toString()));

} catch (NumberFormatException e) {

// TODO: handle exception
e.printStackTrace();
}

f5 = Float.parseFloat(e1.getText().toString());


f3 = (f1 * f2 / 100);

f4 = f3 + f1;
int f6 = (int) (f4 / f5);
int f7 = (int) (f4 % f5);
String s2 = String.valueOf(f6);
tvans.setText(s2 + "    Month      " + f7
+ " Rs "+ "Remaing Money");

}

}
});
}

@Override

public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}

});
}

@Override

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


}

NOTE: no needed any special permission into AndroidManifest.xml


Step 4: Run Your EMI loan converter.




Download @ Play store:


No comments:

Post a Comment