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:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calculator_tablelLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="*">
    <EditText
        android:id="@+id/displayText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:focusable="false"
        android:gravity="right"
        android:inputType="textCapCharacters"
        android:singleLine="true"
        android:text="@string/display_text" 
        android:textSize="40sp">
        <requestFocus android:layout_width="match_parent" />
    </EditText>
    <TableRow
        android:id="@+id/calculator_tableRow1" 
        android:layout_weight="1" >
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_clear"
            android:text="@string/button_clear_text" />
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_sign"
            android:text="@string/button_sign_text"/>
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_root"
            android:text="@string/button_root_text" />
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_divide"
            android:text="@string/button_devide_text" />
    </TableRow>
    <TableRow
            android:id="@+id/calculator_tableRow2"
            android:layout_weight="1" >
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_seven"
            android:text="@string/button_seven_text"/>
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_eight"
            android:text="@string/button_eight_text"/>
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_nine"
            android:text="@string/button_nine_text"/>
        <Button
            style="@style/CalclatorButton" 
            android:id="@+id/button_times"
            android:text="@string/button_times_text"/>      
     </TableRow>
     <TableRow
            android:id="@+id/calculator_tableRow3"
            android:layout_weight="1" >
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_four"
            android:text="@string/button_four_text"/>
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_five"
            android:text="@string/button_five_text" />
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_six"
            android:text="@string/button_six_text" />
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_minus"
            android:text="@string/button_minus_text" />
     </TableRow>
     <TableRow
         android:id="@+id/calculator_tableRow4"
         android:layout_weight="1" >
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_one"
            android:text="@string/button_one_text" />
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_two"
            android:text="@string/button_two_text"/>
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_three"
            android:text="@string/button_three_text"/>
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_plus"
            android:text="@string/button_plus_text"/>
     </TableRow>
     <TableRow
         android:id="@+id/calculator_tableRow5"
         android:layout_weight="1" >
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_null"
            android:layout_weight="0.25"
            android:text="@string/button_null_text"/>
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_point"
            android:layout_weight="0.25"
            android:text="@string/button_point_text" />
        <Button
            style="@style/CalclatorButton"
            android:id="@+id/button_enter"
            android:layout_weight="0.5"
            android:text="@string/button_enter_text"/>
        </TableRow>
</TableLayout>