Friday, February 22, 2013

Loan Calculator in Android




This example is a simple loan calculator application which shows monthly payment and total loan of the values entered.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it LoanCalculator.
2.) Write following into 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
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:stretchColumns="1"
             android:shrinkColumns="1">
    <TableRow>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/loan_amount_prompt"
                  android:gravity="right"/>
        <EditText android:id="@+id/loan_amount"
                  android:inputType="numberDecimal"
                  android:layout_height="wrap_content">
            <requestFocus></requestFocus>
        </EditText>
    </TableRow>
    <TableRow>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/interest_rate_prompt"
                  android:gravity="right"/>
        <EditText android:id="@+id/interest_rate"
                  android:inputType="numberDecimal"
                  android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/loan_period_prompt"
                  android:gravity="right"/>
        <EditText android:id="@+id/loan_period"
                  android:inputType="number"
                  android:layout_height="wrap_content"/>
    </TableRow>
    <TableRow>
        <Button android:text="@string/loan_button_text"
                android:layout_span="2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="showLoanPayments"/>
    </TableRow>
    <TableRow android:layout_marginTop="20dp">
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/monthly_payment_prompt"
                  android:gravity="right"/>
        <TextView android:id="@+id/monthly_payment_result"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#ff0000"
                  android:gravity="left"/>
    </TableRow>
    <TableRow>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="@string/total_payments_prompt"
                  android:gravity="right"/>
        <TextView android:id="@+id/total_payments_result"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:textColor="#ff0000"
                  android:gravity="left"/>
    </TableRow>
</TableLayout>
3.) Write following into values/strings.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">LoanCalculator</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="loan_amount_prompt">Loan amount:&#160;&#160;</string>
    <string name="interest_rate_prompt">Interest rate:&#160;&#160;</string>
    <string name="loan_period_prompt">Months:&#160;&#160;</string>
    <string name="loan_button_text">Calculate Payments</string>
    <string name="monthly_payment_prompt">Monthly payment:&#160;&#160;</string>
    <string name="total_payments_prompt">Total payments:&#160;&#160;</string>
</resources>
4.) Run for output.
Steps:
1.) Create a project named LoanCalculator and set the information as stated in the image.
Build Target: Android 4.0
Application Name: LoanCalculator
Package Name: com. example. LoanCalculator
Activity Name: LoanCalculatorActivity
Min SDK Version: 2.2
2.) Open LoanCalculatorActivity.java file and write following code there:
package com.example.loancalculator;
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
import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class LoanCalculatorActivity extends Activity {
    private EditText mLoanAmount, mInterestRate, mLoanPeriod;
    private TextView mMontlyPaymentResult, mTotalPaymentsResult;
     
    /** Initializes the app when it is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLoanAmount = (EditText)findViewById(R.id.loan_amount);
        mInterestRate = (EditText)findViewById(R.id.interest_rate);
        mLoanPeriod = (EditText)findViewById(R.id.loan_period);
        mMontlyPaymentResult = (TextView)findViewById(R.id.monthly_payment_result);
        mTotalPaymentsResult = (TextView)findViewById(R.id.total_payments_result);
    }
     
    public void showLoanPayments(View clickedButton) {
         
        double loanAmount = Integer.parseInt(mLoanAmount.getText().toString());
        double interestRate = (Integer.parseInt(mInterestRate.getText().toString()));
        double loanPeriod = Integer.parseInt(mLoanPeriod.getText().toString());
        double r = interestRate/1200;
        double r1 =  Math.pow(r+1,loanPeriod);
         
        double monthlyPayment = (double) ((r+(r/(r1-1))) * loanAmount);
        double totalPayment = monthlyPayment * loanPeriod;
         
        mMontlyPaymentResult.setText(new DecimalFormat("##.##").format(monthlyPayment));
        mTotalPaymentsResult.setText(new DecimalFormat("##.##").format(totalPayment));
           }
}
3.) Compile and build the project.
Output

No comments:

Post a Comment