Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Creating a UI Fragment


139

Creating a UI Fragment


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



  • compose a UI by defining widgets in a layout file

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

  • wire up the widgets inflated from the layout in code


Defining CrimeFragment’s layout


CrimeFragment’s view will display the information contained within an instance of Crime.


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


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



CriminalIntent
Enter a title for the crime.
Title
Details
Solved

Next, you will define the UI. The layout for CrimeFragment will consist of a vertical LinearLayout
that contains two TextViews, an EditText, a Button, and a Checkbox.


To create a layout file, right-click the res/layout folder in the project tool window and select New →
Layout resource file. Name this file fragment_crime.xml and enter LinearLayout as the root element.
Click OK and Android Studio will generate the file for you.


http://www.ebook3000.com

Free download pdf