2013/06/11

Model View Controller Pattern in Android (Part 2)

Building the view

As an example for a I want to build a simple RPN Calculator, a Reverse Polish Notation calculator. A RPN calculator is a calculator where you enter all your numbers using an enter key and than the operations pull the numbers back off a stack of operations. So for example, if you wanted to do three plus five you would say three enter, five enter, plus and it would pull the three and the five off, add them, make eight, and then it it would put that back on the stack. And so if you then said six plus you’d get 14.
For building the user interface you obviously need some buttons a text label area so you can have a display in the calculator, and things like that. In the following figure the UI is shown for an Android 2.2 device.

Calculator View for Android 2.2 in Landscape Mode

Calculator View in Portrait Mode

In Android, objects in the view layer are inflated from XML within a layout file. The layout file of the calculator consists of a TableLayout with an EditText field and rows of buttons: