Chapter 9. Fragments: Make It Modular

You’ve seen how to create apps that work in the same way no matter what device they’re running on.
But what if you want your app to look and behave differently depending on whether it’s running on a phone or a tablet? In this case you need fragments, modular code components that can be reused by different activities. We’ll show you how to create basic fragments and list fragments, how to add them to your activities, and how to get your fragments and activities to communicate with one another.
Your app needs to look great on ALL devices
One of the great things about Android development is that you can put the exact same app on devices with completely different screen sizes and processors, and have them run in exactly the same way. But that doesn’t mean that they always have to look exactly the same.
On a phone:
Take a look at this image of an app on a phone. It displays a list of workouts, and when you click on one, you are shown the details of that workout.

On a tablet:
On a larger device, like a tablet, you have a lot more screen space available, so it would be good if all the information appeared on the same screen. On the tablet, the list of workouts only goes partway across the screen, and when you click on an item, the details appear on the right. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access