Using a Layout Fragment

In CriminalIntent, you have been hosting fragments by adding them in the activity’s code. In HelloMoon, you are going to use a layout fragment instead. When you use a layout fragment, you specify the fragment class in a fragment element.

Open activity_hello_moon.xml and replace its contents with the fragment element shown in Listing 13.4.

Listing 13.4  Creating a layout fragment (activity_hello_moon.xml)

<​?​x​m​l​ ​v​e​r​s​i​o​n​=​"​1​.​0​"​ ​e​n​c​o​d​i​n​g​=​"​u​t​f​-​8​"​?​>​
<​f​r​a​g​m​e​n​t​ ​x​m​l​n​s​:​a​n​d​r​o​i​d​=​"​h​t​t​p​:​/​/​s​c​h​e​m​a​s​.​a​n​d​r​o​i​d​.​c​o​m​/​a​p​k​/​r​e​s​/​a​n​d​r​o​i​d​"​
 ​ ​a​n​d​r​o​i​d​:​i​d​=​"​@​+​i​d​/​h​e​l​l​o​M​o​o​n​F​r​a​g​m​e​n​t​"​
 ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​w​i​d​t​h​=​"​m​a​t​c​h​_​p​a​r​e​n​t​" ...

Get Android Programming: The Big Nerd Ranch Guide 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.