Intro to Android

Lab: Display View

Add the view

Replace the LinearLayout from previous steps with this EditText in display.xml.

We can do this since each view expects only one view at the root, but may have more. Since our view is so simple, there is no need to have an enclosing view group.

<EditText
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/display"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="20dp"
    android:paddingBottom="20dp"
    android:gravity="center"
    android:textSize="40sp"
    android:textStyle="bold"
    android:textColor="@color/nearly_black"
    android:editable="false"
    android:maxLength="10"
    android:inputType="none"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:background="@android:color/white"/>

Here I've added many XML attributes! The attributes are mostly self explanatory, but I'd be happy to answer any questions on particular attributes.

We'll learn more about abstracting some of the hard coded values out later into dimensions and styles.

Add colors.xml

For the background color I'm using a built in Android resource:

android:background="@android:color/white"

For the text color I defined a new color in res/values/colors.xml:

<resources>
    <color name="nearly_black">#ff313131</color>
</resources>

Add test to DisplayFragmentTest.java

We going to refactor a bit here to make it easier to test new items. First, make a field, fragment, for the fragment under test. Then, move the fragment initialization and fragment starting to the setUp() function.

public class DisplayFragmentTest
{
    private DisplayFragment fragment;

    @Before
    public void setUp() throws Exception
    {
        fragment = new DisplayFragment();
        startFragment( fragment );
    }

    @Test
    public void shouldNotBeNull() throws Exception
    {
        assertNotNull( fragment );
    }
}

Now we're ready to add the new test:

@Test
public void shouldHaveDefaultDisplay() throws Exception
{
    EditText display = (EditText) fragment.getView().findViewById( R.id.display );
    assertViewIsVisible( display );
    assertThat( display.getText().toString(),
                equalTo( "" ) );
}