Real-world apps

So far, we have built a dozen or more apps of various complexity. Most were designed and tested on a phone.

Of course, in the real world, our apps need to work well on any device and must be able to handle what happens when in either portrait or landscape view (on all devices).

Furthermore, it is often not enough for our apps to just work and look OK on different devices. Often, our apps will need to behave differently and appear with significantly different UI based on whether the device is a phone, a tablet, or landscape/portrait orientation.

Note

Android supports apps for large screen TVs, smart watches via the Wear API, virtual and augmented reality, as well as "things" for the Internet of Things. We will not be covering the latter two aspects in this book but, by the end of it, it is the author's guess that you will be sufficiently prepared to venture into these topics should you choose to.

Look at this screenshot of the BBC news app running on an Android phone in portrait orientation. Look at the basic layout, but also notice that the categories of news (Top Stories, World, UK) are all visible and allow the user to scroll to see more categories, or to swipe left and right between the stories within each category:

Real-world apps

We will see how we can implement a swiping/paging UI using ImagePager and FragmentPager classes in the next chapter, but before we can do that, we need to understand some more fundamentals that we explore in this chapter. For now, the purpose of the previous image is not so much to show you the specific UI features, but to allow you to compare it with the next image. Look at the exact same app running on a tablet in landscape orientation:

Real-world apps

Notice that the stories (data layer) are identical, but that the layout (view layer) is very different. The user is not only given the option to select categories from a menu of tabs at the top of the app; they are also invited to add their own tabs through the Add Topics option.

The point of this image again is not so much the specific UI, or even how we might implement one like it, but more that they are so different they could easily be mistaken for totally different apps.

Android allows us to design real-world apps like this where not only is the layout different for varying device types/orientations/sizes, but so is the behavior, the application layer. Android's secret weapon that makes this possible is Fragments.

Note

Google says

"A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities.

You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

A fragment must always be embedded in an activity, and the fragment's lifecycle is directly affected by the host activity's lifecycle."

We can design multiple different layouts in different XML files, and will do so soon. We can also detect things such as device orientation and screen resolution in code, so we can then make decisions about layout, dynamically.

Let's try this out using device detection and then we will have our first look at Fragments.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.56.194