Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Android Studio 3

You're reading from  Mastering Android Studio 3

Product type Book
Published in Aug 2017
Publisher
ISBN-13 9781786467447
Pages 220 pages
Edition 1st Edition
Languages
Author (1):
Kyle Mew Kyle Mew
Profile icon Kyle Mew

Device Development

Android Studio provides some very powerful layout tools, enabling us to quickly and easily experiment with and develop user interfaces. However, perhaps the biggest challenge any Android developer faces is the bewildering number of form factors their applications could run on.

We saw in previous chapters how classes, for example the constraint layout and libraries such as the percent library, help us design uniform and consistent layouts. However, these techniques only provide general solutions, and we will all have stumbled across apps that do not really seem to have been designed with our device in mind. With a little knowledge and effort, these design faults can easily be avoided.

In this chapter, you will learn to:

  • Create alternative layout files
  • Extract string resources
  • Manage screen rotation
  • Configure resources
  • Create wearable UIs
  • Build shape-aware layouts...

Screen orientation

A large percentage of Android apps designed for phones and tablets are designed to work in both landscape and portrait mode and generally switch between these automatically. Many activities, such as videos, are best viewed in landscape mode, while lists are usually easier to scan in portrait; however, and there are some activities, and even entire apps, where the orientation is fixed.

There are some layouts that look equally good, whichever way they are viewed, but this is not often the case; most of the time, we will want to design one for each orientation. Android Studio simplifies and speeds up this process by saving us the task of developing an alternative layout from scratch.

Take a simple layout like the one here:

A portrait layout

A landscape variant can be created with a single click from the Layout Variant tool at the top of the design editor, as follows...

Virtual devices

For a long time, Android Virtual Devices (AVDs) had the reputation of being buggy and horrendously slow. The introduction of hardware acceleration has made a big difference, but a powerful computer is still advised, especially if you want to run more than one at a time, which is very often the case.

The biggest change to Android emulation is not hardware acceleration, but rather the appearance of alternative emulators. As we shall see shortly, some of these offer distinct advantages over the native emulator, but AVDs should not be written off. Despite the drawbacks, Android emulators are the only emulators that run on all Android versions, including the most recent, developer-only, versions. Not only this, but Android emulators are the most customizable and any possible hardware or software configuration can be recreated with a little effort.

Early on in the development...

Android Wear

Wearable devices have become very popular of late and Android Wear is fully incorporated into the Android SDK. The setting up of a Wear project is slightly more involved than other projects as wearable devices really act as a companion device with the apps themselves running from a mobile device.

Despite this minor level of complication, developing for wearables can be a lot of fun, not least because they often offer us access to some cool sensors, such as the heart rate monitor.

Connecting to a wearable AVD

It may well be that you have access to a wearable device, but here we will be using emulators in the following exercise. This is because these devices come in two flavors: square and round.

When it comes to...

Accessing sensors

Devices worn on the wrist are ideal for fitness apps and the inclusion of a heart rate monitor in many models makes them perfect for such tasks. The way that the SDK manages all sensors is almost identical, so seeing how one works applies to the others.

The following exercise demonstrates how to read the heart rate sensor on a wearable device:

  1. Open an Android Wear project with both mobile and wear modules.
  2. Create a layout of your choosing, ensuring you include a TextView to display the output.

  1. Open the Manifest file in the wear module and add the following permission:
<uses-permission 
android:name="android.permission.BODY_SENSORS" />
  1. Open the MainActivity.java file in the wear module and add the following fields:
private TextView textView; 
private SensorManager sensorManager;
private Sensor sensor;
  1. Have the Activity implement a...

Device monitoring

Very often, simply running an app on a device or emulator is enough to tell us if what we have designed works and what, if anything, we need to change. However it is always great to see what is going on under the hood, and Android Studio has some fantastic tools when it comes to the live monitoring of an app's behavior.

We will cover debugging in detail in the next module, but it is never too soon to play with the Android Debug Bridge (ADB) and Android Studio's Device Monitor tool is one of the most significant benefits of choosing the IDE over the alternatives.

This section also offers a good opportunity to take a closer look at project templates, another fantastic feature of Android Studio.

Project templates

...

Summary

This chapter has covered a lot of ground; we have taken the work done on Android layouts in an earlier chapter and begun to explore how these can be taken from static graphics to more dynamic structures. We have seen how Android provides classes and libraries that make developing for different screens easier than with other IDEs and how the emulator can be used to produce all possible form factors, including the most recent platforms.

There is only one more chapter in this module on layout and design before we move onto coding; in it we will cover how the numerous resources available to us are managed and how Android Studio assists us in this.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Mastering Android Studio 3
Published in: Aug 2017 Publisher: ISBN-13: 9781786467447
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime}