Wednesday, February 15, 2012

Loan Calculator Phase 1, Iteration 2

In this iteration, we are going to implement the amortization function that including the addition and enhancement of the following from LoanCalculator_1_1

- Add amortization function to the model object LoanCalculator.
- Create a Scroll Pane (horizontal and vertical scrolling table) with resource layout XML.
- Create an Activity class (i.e. AmortizationActivity) to render amortization schedule


LoanCalculator domain object

LoanCalculator is passed among activities as a Serializable in Bundle. The class is modified to implement Serializable interface.

public class LoanCalculator implements Serializable {

}

Added the computeAmortization() method that returns an array of String array (i.e. String[][]). The String[][] represents the amortization schedule having rows of amortization period, opening balance, principal per payment, interest per payment, accumulated principal payment, accumulated interest payment and close balance.

public String[][] computeAmortization(){

}


Listing 1 – LoanCalculator.java


Scroll Pane layout XML

There’s not enough real estate in phone’s display to show the amortization schedule, we need a view that can scroll horizontally and vertically. This can be achieved by nesting a HorizontalScrollView inside a ScrollView in layout XML.


<ScrollView>
<HorizontalScrollView>
<TableLayout>
<TableRow>
<TextView
android:text="@string/amortization_1"
android:padding="3dip"
/>
...
...
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>

Listing 2 – amortization.xml


Please refer to my previous Blog posting – “Howto – Create a two dimensions scroll view” for details.


AmortizationActivity Activity class

The AmortizationActivity extends from Activity class. Remember to register new activity in AndroidManifest.xml, such as…

<activity android:name=".AmortizationActivity"></activity>

onCreate() method
Retrieve the serialized calculator object passed from the invoking Activity, calls the calculator’s computeAmortization() for amortization schedule and invoke appendRows() to populate the scroll pane.

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.amortization);
TableLayout aTable=(TableLayout)findViewById(R.id.amortization);
Bundle aBundle=getIntent().getExtras();
if(aBundle!=null){
LoanCalculator amortization= (LoanCalculator)aBundle.getSerializable("amortization");
String[][] amortValues=amortization.computeAmortization();
appendRows(aTable,amortValues);
}
}


appendRows() method
Programmatically create TableRow (with multiple TextView in each row) based on the calculated amortization schedule. Created TableRow is added to the TableLayout that is scrollable horizontally in the HorizontalScrollView, whereas the HorizontalScrollView is vertically scrollable in the ScrollView.

private void appendRows(TableLayout table, String[][] amortization) {
int rowSize=amortization.length;
int colSize=(amortization.length>0)?amortization[0].length:0;
for(int i=0; i<rowSize; i++){
TableRow row = new TableRow(this);
for(int j=0; j<colSize; j++){
TextView c = new TextView(this);
c.setText(amortization[i][j]);
c.setPadding(3, 3, 3, 3);
row.addView(c);
}

table.addView(row, new TableLayout.LayoutParams());
}
}

Listing 3 – AmortizationActivity.java


Enhancement on LoanCalculatorActivity

A button Listener is created for the Click event from the Amortization button. In the Listener, the calculator object is populated and passed to the AmortizationActivity through the Intent’s Bundle.

private void addAmortizationListener(){
Button getRatesButton=
(Button)findViewById(R.id.b_calcAmortization);
getRatesButton.setOnClickListener(
new View.OnClickListener(){
public void onClick(View view){
Intent anIntent=new Intent(view.getContext(), AmortizationActivity.class);
mCalculator.setPrincipal(…); mCalculator.setAnnualRate(…);
mCalculator.setPeriodsPerYear(…);
mCalculator.setYears(…);
anIntent.putExtra("amortization", mCalculator);
startActivity(anIntent);
setResult(RESULT_OK);
}
}
);
}

List 4 - LoanCalculatorActivity.java

DOWNLOAD - LoanCalculator_1_2.zip Eclipse project