Create a new project in Android Studio, entering FragmentExample into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
 
Create a new project in Android Studio, entering FragmentExample into the Application name field and ebookfrenzy.com as the Company Domain setting before clicking on the Next button.
   −
On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 8: Android 2.2 (Froyo). Continue to proceed through the screens, requesting the creation of a blank activity named FragmentExampleActivity with a corresponding layout named activity_fragment_example.
+
On the form factors screen, enable the Phone and Tablet option and set the minimum SDK setting to API 8: Android 2.2 (Froyo). Continue to proceed through the screens, requesting the creation of a blank activity named FragmentExampleActivity with corresponding layout and menu resource files named activity_fragment_example and menu_fragment_example respectively.
    
Click the Finish button to begin the project creation process.
 
Click the Finish button to begin the project creation process.
 
The next step is to create the user interface for the first fragment that will be used within our activity.  
 
The next step is to create the user interface for the first fragment that will be used within our activity.  
   −
This user interface will, of course, reside in an XML layout file so begin by navigating to the layout folder located under FragmentExample -> app -> src -> main -> res in the Project tool window. Once located, right-click on the layout entry and select the New -> Layout resource file menu option as illustrated in Figure 25-1:
+
This user interface will, of course, reside in an XML layout file so begin by navigating to the layout folder located under app -> res in the Project tool window. Once located, right-click on the layout entry and select the New -> Layout resource file menu option as illustrated in Figure 25-1:
      −
[[Image:android_studio_create_new_layout_file.png|Adding a layout resource file to an Android Studio project]]
+
[[Image:android_studio_create_new_layout_file2.png|Adding a layout resource file to an Android Studio project]]
 
   
 
   
 
Figure 25-1
 
Figure 25-1
      −
[[Image:android_studio_fragment_one.png|The user interfaced layout for the toolbar fragment]]
+
[[Image:android_studio_fragment_one2.png|The user interfaced layout for the toolbar fragment]]
 
   
 
   
 
Figure 25-2
 
Figure 25-2
 
== Creating the First Fragment Class ==
 
== Creating the First Fragment Class ==
   −
In addition to a user interface layout, a fragment also needs to have a class associated with it to do the actual work behind the scenes. Add a class for this purpose to the project by unfolding the FragmentExample -> app -> src -> main -> java folder under the FragmentExample project in the Project tool window and right-clicking on the package name given to the project when it was created (in this instance com.ebookfrenzy.fragmentexample). From the resulting menu, select the New -> Java Class option. In the resulting Create New Class dialog, name the class ToolbarFragment and click on OK to create the new class.
+
In addition to a user interface layout, a fragment also needs to have a class associated with it to do the actual work behind the scenes. Add a class for this purpose to the project by unfolding the app -> java folder under the FragmentExample project in the Project tool window and right-clicking on the package name given to the project when it was created (in this instance com.ebookfrenzy.fragmentexample). From the resulting menu, select the New -> Java Class option. In the resulting Create New Class dialog, name the class ToolbarFragment and click on OK to create the new class.
    
Once the class has been created, it should, by default, appear in the editing panel where it will read as follows:
 
Once the class has been created, it should, by default, appear in the editing panel where it will read as follows:
 
   
 
   
   −
[[Image:android_studio_fragment_two_ui.png|The second Fragment UI layout in Android Studio Designer]]
+
[[Image:android_studio_fragment_two_ui2.png|The second Fragment UI layout in Android Studio Designer]]
    
Figure 25-3
 
Figure 25-3
      Exception encountered, of type "Error"
[9598db1c] /index.php?title=Using_Fragments_in_Android_Studio_-_An_Example&diff=18059&oldid=17354 Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/includes/diff/DifferenceEngine.php(881): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): DifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff()
#14 /var/www/techotopia/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff()
#15 /var/www/techotopia/includes/page/Article.php(797): DifferenceEngine->showDiffPage()
#16 /var/www/techotopia/includes/page/Article.php(508): Article->showDiffPage()
#17 /var/www/techotopia/includes/actions/ViewAction.php(44): Article->view()
#18 /var/www/techotopia/includes/MediaWiki.php(490): ViewAction->show()
#19 /var/www/techotopia/includes/MediaWiki.php(287): MediaWiki->performAction()
#20 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#21 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#22 /var/www/techotopia/index.php(41): MediaWiki->run()
#23 {main}