Book Image

Creating Dynamic UIs with Android Fragments - Second Edition

By : Jim Wilson
Book Image

Creating Dynamic UIs with Android Fragments - Second Edition

By: Jim Wilson

Overview of this book

Today’s users expect mobile apps to be dynamic and highly interactive, with rich navigation features. These same apps must look fantastic whether running on a medium-resolution smartphone or high-resolution tablet. Fragments provide the toolset we need to meet these user expectations by enabling us to build our applications out of adaptable components that take advantage of the rich capabilities of each individual device and automatically adapt to their differences. This book looks at the impact fragments have on Android UI design and their role in both simplifying many common UI challenges and in providing best practices for incorporating rich UI behaviors. We look closely at the roll of fragment transactions and how to work with the Android back stack. Leveraging this understanding, we explore several specialized fragment-related classes such as ListFragment and DialogFragment. We then go on to discuss how to implement rich navigation features such as swipe-based screen browsing, and the role of fragments when developing applications that take advantage of the latest aspects of Material Design. You will learn everything you need to provide dynamic, multi-screen UIs within a single activity, and the rich UI features demanded by today’s mobile users.
Table of Contents (13 chapters)


Long gone are the days of mobile apps with a static UI squished on a tiny screen. Today's users expect mobile apps to be dynamic and highly interactive. They expect an app to look fantastic when they look at it on their medium resolution smartphone and just as fantastic when they switch over to using it on their high-resolution tablet. Apps need to provide rich navigation features, be adaptive, and be responsive.

Trying to meet these demands using Android's traditional activity-centric UI design model is difficult at best. As developers, we need more control than that afforded by activities. We need a new approach, and fragments give us this new approach.

In this book, you'll learn how to use fragments to meet the challenges of creating dynamic UIs in the modern world of mobile app development.

What this book covers

Chapter 1, Fragments and UI Modularization, introduces fragments, UI modularization, and the role that fragments play in developing a modularized UI. This chapter demonstrates creating simple fragments and using fragments statically within activities.

Chapter 2, Fragments and UI Flexibility, builds on the concepts introduced in the previous chapter to provide solutions to specific differences in device layouts. This chapter explains how to use adaptive activity layout definitions to provide support for a wide variety of device form factors with a small set of fragments that are automatically rearranged based on the current device's UI requirements.

Chapter 3, Fragment Life Cycle and Specialization, discusses the relationship of the life cycle of fragments with that of activities and demonstrates the appropriate programming actions at the various points in the life cycle. Leveraging this knowledge, the special purpose fragment classes, ListFragment and DialogFragment, are introduced to demonstrate their behavior and provide a deeper understanding of how their behavior in the activity life cycle differs from that of standard fragments.

Chapter 4, Working with Fragment Transactions, explains how to create multiple app screens within a single activity by dynamically adding and removing fragments using fragment transactions. Topics covered include thread handling, implementing back button behavior, and dynamically adapting multifragment UIs to differences in device characteristics.

Chapter 5, Creating Rich Navigation, brings everything together by building on the previous chapters to show how to use fragments to enhance the user's experience through rich navigation features. This chapter demonstrates how to implement a number of navigation features, including screen browsing with swipe-based paging, direct screen access with drop-down list navigation, and random screen viewing with tabs.

Chapter 6, Fragments and Material Design, introduces the next generation of application development using material design. This chapter demonstrates how to implement fragments that incorporate a rich visual appearance and animated transitions using the latest features of Android's material design capabilities.

What you need for this book

To follow the examples in this book, you should have a basic knowledge of Android programming and a working Android development environment.

This book focuses primarily on Android Studio and the Android development environment; however, other tools, such as Eclipse with the ADT plugin, JetBrains' IntelliJ IDEA IDE, or a similar Android-enabled development tool, can also be used.

Who this book is for

This book is for anyone with a basic understanding of Android programming who would like to improve the appearance and usability of their applications.

Whether you're looking to create a more interactive user experience, create more dynamically adaptive UIs, provide better support for tablets and smartphones in a single app, reduce the complexity of managing your app UIs, or just trying to expand your UI design philosophy, this book is for you.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To create a fragment for the book list, we will define a new layout resource file called fragment_book_list.xml."

A block of code is set as follows:

public class BookListFragment extends Fragment
  implements RadioGroup.OnCheckedChangeListener {
  public void onCheckedChanged(RadioGroup radioGroup, int id) {
  // Other members elided for clarity

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

public class BookListFragment extends Fragment
  implements RadioGroup.OnCheckedChangeListener {
  public void onCheckedChanged(RadioGroup radioGroup, int id) {
  // Other members elided for clarity

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In Android Studio, we associate a resource file with this qualifier by selecting Screen Height in the New Resource File dialog."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

Downloading the color images of this book

We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.