Book Image

Creating Dynamic UI with Android Fragments

By : Jim Wilson
Book Image

Creating Dynamic UI with Android Fragments

By: Jim Wilson

Overview of this book

To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle. When a fragment specifies its own layout, it can be configured in different combinations with other fragments inside an activity to modify your layout configuration for different screen sizes (a small screen might show one fragment at a time, but a large screen can show two or more). Creating Dynamic UI with Android Fragments shows you how to create modern Android applications that meet the high expectations of today's users. You will learn how to incorporate rich navigation features like swipe-based screen browsing and how to create adaptive UIs that ensure your application looks fantastic whether run on a low cost smartphone or the latest tablet. This book looks at the impact fragments have on Android UI design and their role in both simplifying many common UI challenges and providing new ways to incorporate rich UI behaviors. You will learn how to use fragments to create UIs that automatically adapt to device differences. We look closely at the roll of fragment transactions and how to work with the Android back stack. Leveraging this understanding, we then explore several specialized fragment-related classes like ListFragment and DialogFragment as well as rich navigation features like swipe-based screen browsing.
Table of Contents (13 chapters)

Preface

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

Trying to meet these demands using Android's traditional activity-centric UI design model is difficult. As developers, we need more control than that afforded by activities. We need a new approach: fragments give us that 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 fragments play in developing a modularized UI. This chapter demonstrates the creation of 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 automatically rearrange based on the current device's UI requirements.

Chapter 3, Fragment Lifecycle and Specialization, discusses the relationship of the lifecycle of fragments to that of activities, and demonstrates the appropriate programming actions at the various points in the lifecycle. 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 lifecycle 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, implementing back button behavior and dynamically adapting multi-fragment UIs to differences in device characteristics.

Chapter 5, Creating Rich Navigation with Fragments, 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 the drop-down list navigation, and random screen viewing with tabs.

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 as the Android development environment, but other tools such as Eclipse with the ADT plugin, JetBrains' IntelliJ IDEA, or a similar Android-enabled development tool can 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.

Conventions

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

Code words in text are shown as follows: "An application initially calls the startActivity method to display an instance of Activity1. Activity1."

A block of code is set as follows:

<string-array name="screen_names">
  <item>First View</item>
  <item>Second View</item>
  <item>Third View</item>
</string-array>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Select layout as the Resource type."

Note

Warnings or important notes appear in a box like this.

Tip

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on www.packtpub.com/authors.

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 all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

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 would 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 http://www.packtpub.com/submit-errata, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.