Está en la página 1de 73
22. Using Fragments in Android - A Worked Example As outlined in the previous chapter, fragments provide a convenient mechanism for creating reusable modules of application functionality consisting of both sections of a user interface and the corresponding behavior. Once created, fragments can be embedded within activities. Having explored the overall theory of fragments in the previous chapter, the objective of this chapter is to create an example Android application designed to demonstrate the actual steps involved in both creating and using fragments, and also implementing communication between one fragment and another within an activity. 22.1 About the Example Fragment Application ‘The application created in this chapter will consist of asingle activity and two fragments. The user interface for the first fragment will contain a toolbar of sorts consisting of an EditText view, a SeekBar and a Button, all contained within a RelativeLayout view. The second fragment will consist solely ofa ‘TextView object, also contained within a RelativeLayout view. ‘The two fragments will be embedded within the main activity of the application and communication implemented such that when the button in the first fragment is pressed, the text entered into the ‘EditText view will appear on the TextView of the second fragment using a font size dictated by the position of the SeekBar in the first fragment. Since this application is intended to work on earlier versions of Android, it will also be necessary to make use of the appropriate Android support library. 22.2 Creating the Example Project Launch Eclipse and create an Android Application Project named FragmentExample with the appropriate package name and SDK selections. As with previous examples, request the creation ofa blank activity and the use of the default launcher icons. On the New Blank Activity screen of the New ‘Android Application wizard, set the Activity Name to FragmentExampleActivity and the Layout Name to activity_fragment_example. 22.3 Adding the Android Support Library ‘As previously discussed, support for fragments was not introduced until Android 3.0. This means that for applications that make use of fragments to be ‘compatible with older versions of Android, use must be made of version 4 of the Android Support Library. The purpose of the Android Support Library is to make features that were introduced in later versions of Android available to applications that need to be compatible with earlier Android versions. Assuming that the installation steps outlined in the chapter entitled Setting up an Android Development Environment were followed, then the Android Support Library package should already be installed onyour development system. To verify this, launch Eclipse and select the Window -> Android SDK ‘Manager menu option. When the manager window has appeared on the screen, scroll down to the Extras section and make sure that the column to the far right of the Android Support Library item is, listed as Installed as shown in Figure 22-1: oa - -oe Andkold SOK Manager Stor Slibaneihe Slistaed ClOmcew Sectcn © Ubaus [hia paape Soc At OReeaty Desc ne oi pcoes Figure 22-1 With the Android Support Library installed, the next step is to verify that it is already included in the project. Remaining within the Eclipse environment, locate the FragmentExample project within the Package Explorer panel and unfold the libs folder. If the support library is already included in the project, it will be listed as android-support-v4,jar: 4 WS Fagmentécample Bx 5 gen [Generated Java Fites] BA Android 4.2 BA Android Dependencies i assets B tin 4 & ibe {@) AndroidManitestamt ic launcher-web,png preguard-project-t reject properties Figure 29-2 If the support library package is not listed, it will need to be added before proceeding with this. chapter. This involves locating the jar file in the Android SDK installation directory and placing a copy in the libs folder of the project. Begin by using the file system browser of your operating system to navigate to the following location (where is the directory into which the ADT Bundle was originally installed when you set up your development environment): /sdk/extr- as/android/support/v4 Within the above folder will be a file named android-support-v4jar. Simply lick and drag this file and drop it onto the libs folder within the FragmentExample project in the Eclipse Package Explorer panel. When prompted by the File Operation panel, select the option to copy the file into the project before clicking OK. ‘The support library is now installed within the project and is ready to be used.

También podría gustarte