Intro to Android

Lab: Add ButtonFragment

Your turn! Please complete this task with your partner.

Create ButtonFragment.java

public class ButtonFragment extends Fragment
{
    @Override
    public View onCreateView( LayoutInflater inflater,
                              ViewGroup container,
                              Bundle savedInstanceState )
    {
        View layout = inflater.inflate( R.layout.buttons,
                                        container,
                                        false );
        return layout;
    }
}

Create the view

The XML view doesn't exist, so we'll create a file called buttons.xml in the src/layout/ folder. We'll add the specifics of the view later.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>

Create ButtonFragmentTest.java

@RunWith (RobolectricTestRunner.class)

public class ButtonFragmentTest
{
    @Test
    public void shouldNotBeNull() throws Exception
    {
        ButtonFragment fragment = new ButtonFragment();
        startFragment( fragment);
        assertNotNull( fragment );
    }
}

Add to view

Let's add our fragment to main.xml below the DisplayFragment.

<fragment
    android:id="@+id/buttons_fragment"
    android:name="com.mobiquity.calculator.ButtonFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Test presence in the activity

@Test
public void shouldHaveButtonsFragment() throws Exception
{
    assertNotNull( activity.getFragmentManager()
                           .findFragmentById( R.id.buttons_fragment ) );
}

Test & commit

Make sure to run the tests and then commit your work to your repository.