Creating a Fragment

The steps to create a fragment are the same as those you followed to create an activity:

  • compose a UI by defining views in a layout file

  • create the class and set its view to be the layout that you defined

  • wire up the views inflated from the layout in code

Defining CrimeDetailFragment’s layout

CrimeDetailFragment’s view will display the information contained in an instance of Crime.

First, define the strings that the user will see in res/values/strings.xml.

Listing 9.3  Adding strings (res/values/strings.xml)

<resources>
    <string name="app_name">CriminalIntent</string>
    <string name="crime_title_hint">Enter a title for the crime.</string>
    <string name="crime_title_label">Title</string>
    

Get Android Programming: The Big Nerd Ranch Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.