Android Adventures - Fragments And XML
Written by Mike James   
Thursday, 27 February 2014
Article Index
Android Adventures - Fragments And XML
Blank Activity Template
Summary

Leave it to Android Studio

To add the Fragment to the Activity's XML layout the easiest thing to do is let Android Studio do most of the work.

Load the Activity's XML Layout and switch from the Design tab to the Text Tab

<fragment
 android:name="com.example.app.myFragment"
/>

The name attribute is used to specify the Fragment class that is used to create the View hierarchy - in this case myFragment.java. 

Don't worry about any error messages you see in other windows at this stage. This minimal XML isn't enough to embed the Fragment in the Activity's layout, but Android Studio will fix the problems for you. 

When you switch back to the Designer you will see some attribute errors reported as Rending Errors. It will say that there are missing layout attributes. Simply click the link "Automatically add all missing attributes":

 

fragxmlerror

 

After this the XML you have created is enough to embed the Fragment correctly but there is one final extra touch.

If you look below the error messages you will see advice to the effect that you can associate a layout with the Fragment class and the Designer will render this for you in the Activity's layout so you can see how things are looking. Simply select the name of the layout you would like to use @layout/myfragment in this case. 

If you now look at the XML again you will see that the fragment tag is: 

<fragment
 android:name="com.example.app.myFragment"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 tools:layout="@layout/my_fragment"
/>

Switching back to the Designer now allows you to not only see the UI created by the Fragment, but also to manipulate it within the Activity's layout. You can't edit the internal details of the Fragment, i.e. the Button or TextView in this case. To do this you need to switch your attention back to the Fragment's layout file. It can get confusing but just think of the Fragment as a single widget.

You could run the app at this point and try it out, but it is a good idea to assign a unique id to the Fragment. The reason is that the system uses this id to keep track of the Fragment when it destroys and recreates it again. To assign an id you can use the Designer and the property window again and enter something like: 

 @+id/fragment1

If you don't supply an id the id of the container is used.

If you now run the app you will see the UI generated by the Fragment mixed in with any UI you have created within the Activity. 

It is also worth noting that the Designer can be used to add other types of Fragment into a layout - more about this later. 

Summary

  • A Fragment can create a UI either in code or by inflating an XML Layout - there is no difference.

  • The Blank Activity Template creates its UI within a Fragment but it includes the Fragment class within the Activity's class file. Use Android Studio to refactor to a separate class file.

  • If you want to support Android systems before Honeycomb i.e. 3.0 then you need to make use fo the support libraries. This involves making two changes to your code and importing the support libraries. 

  • Once a Fragment class has been defined you can include it in an Activity's XML Layout file. This is simpler in that you don't have to change the Activity's code, but the Fragment cannot be unloaded or managed in any other way.

  • Android Studio has the ability to let you work with Fragments in the Designer. This is likely to get better and more extensive as it is improved.

Where Next?

So far we know how to create Fragment classes and their UIs in either code or XML and we know how to include a Fragment in the Activity's layout either in code or using XML. 

The next big task is to find out how the Fragment and Activity can communicate results and parameters. This turns out also to be the time to examine the full Fragment template that Android Studio provides. Androidgears

Android Adventures With Android Studio

Contents

  1. Getting Started With Android Studio
  2. The Activity And The UI
  3. Building The UI and a Calculator App
  4. Lifecycle and State
  5. Basic Controls And Events
  6. Spinners
  7. Pickers
  8. UI Graphics A Deep Dive
  9. Introducing Fragments
  10. Fragments and XML
  11. Fragment And Activity Working Together
  12. Managing Fragments
  13. Custom dialogs using DialogFragment
  14. Dialog Classes In DialogFragment

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter,FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 

blog comments powered by Disqus



Last Updated ( Friday, 20 June 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.