Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Android Studio 4.1 Development Essentials – Java Edition

You're reading from  Android Studio 4.1 Development Essentials – Java Edition

Product type Book
Published in May 2021
Publisher Packt
ISBN-13 9781801815161
Pages 810 pages
Edition 1st Edition
Languages
Author (1):
Neil Smyth Neil Smyth
Profile icon Neil Smyth

Table of Contents (88) Chapters

1. Introduction 2. Setting up an Android Studio Development Environment 3. Creating an Example Android App in Android Studio 4. Creating an Android Virtual Device (AVD) in Android Studio 5. Using and Configuring the Android Studio AVD Emulator 6. A Tour of the Android Studio User Interface 7. Testing Android Studio Apps on a Physical Android Device 8. The Basics of the Android Studio Code Editor 9. An Overview of the Android Architecture 10. The Anatomy of an Android Application 11. An Overview of Android View Binding 12. Understanding Android Application and Activity Lifecycles 13. Handling Android Activity State Changes 14. Android Activity State Changes by Example 15. Saving and Restoring the State of an Android Activity 16. Understanding Android Views, View Groups and Layouts 17. A Guide to the Android Studio Layout Editor Tool 18. A Guide to the Android ConstraintLayout 19. A Guide to using ConstraintLayout in Android Studio 20. Working with ConstraintLayout Chains and Ratios in Android Studio 21. An Android Studio Layout Editor ConstraintLayout Tutorial 22. Manual XML Layout Design in Android Studio 23. Managing Constraints using Constraint Sets 24. An Android ConstraintSet Tutorial 25. A Guide to using Apply Changes in Android Studio 26. An Overview and Example of Android Event Handling 27. Android Touch and Multi-touch Event Handling 28. Detecting Common Gestures using the Android Gesture Detector Class 29. Implementing Custom Gesture and Pinch Recognition on Android 30. An Introduction to Android Fragments 31. Using Fragments in Android Studio - An Example 32. Modern Android App Architecture with Jetpack 33. An Android Jetpack ViewModel Tutorial 34. An Android Jetpack LiveData Tutorial 35. An Overview of Android Jetpack Data Binding 36. An Android Jetpack Data Binding Tutorial 37. An Android ViewModel Saved State Tutorial 38. Working with Android Lifecycle-Aware Components 39. An Android Jetpack Lifecycle Awareness Tutorial 40. An Overview of the Navigation Architecture Component 41. An Android Jetpack Navigation Component Tutorial 42. Creating and Managing Overflow Menus on Android 43. An Introduction to MotionLayout 44. An Android MotionLayout Editor Tutorial 45. A MotionLayout KeyCycle Tutorial 46. Working with the Floating Action Button and Snackbar 47. Creating a Tabbed Interface using the TabLayout Component 48. Working with the RecyclerView and CardView Widgets 49. An Android RecyclerView and CardView Tutorial 50. A Layout Editor Sample Data Tutorial 51. Working with the AppBar and Collapsing Toolbar Layouts 52. An Android Studio Master/Detail Flow Tutorial 53. An Overview of Android Intents 54. Android Explicit Intents – A Worked Example 55. Android Implicit Intents – A Worked Example 56. Android Broadcast Intents and Broadcast Receivers 57. A Basic Overview of Threads and AsyncTasks 58. An Overview of Android Started and Bound Services 59. Implementing an Android Started Service – A Worked Example 60. Android Local Bound Services – A Worked Example 61. Android Remote Bound Services – A Worked Example 62. An Android Notifications Tutorial 63. An Android Direct Reply Notification Tutorial 64. Foldable Devices and Multi-Window Support 65. An Overview of Android SQLite Databases 66. The Android Room Persistence Library 67. An Android TableLayout and TableRow Tutorial 68. An Android Room Database and Repository Tutorial 69. Accessing Cloud Storage using the Android Storage Access Framework 70. An Android Storage Access Framework Example 71. Video Playback on Android using the VideoView and MediaController Classes 72. Android Picture-in-Picture Mode 73. An Android Picture-in-Picture Tutorial 74. Making Runtime Permission Requests in Android 75. Android Audio Recording and Playback using MediaPlayer and MediaRecorder 76. Working with the Google Maps Android API in Android Studio 77. Printing with the Android Printing Framework 78. An Android HTML and Web Content Printing Example 79. A Guide to Android Custom Document Printing 80. An Introduction to Android App Links 81. An Android Studio App Links Tutorial 82. A Guide to the Android Studio Profiler 83. An Android Biometric Authentication Tutorial 84. Creating, Testing and Uploading an Android App Bundle 85. An Overview of Android Dynamic Feature Modules 86. An Android Studio Dynamic Feature Tutorial 87. An Overview of Gradle in Android Studio Index

4. Creating an Android Virtual Device (AVD) in Android Studio

In the course of developing Android apps in Android Studio it will be necessary to compile and run an application multiple times. An Android application may be tested by installing and running it either on a physical device or in an Android Virtual Device (AVD) emulator environment. Before an AVD can be used, it must first be created and configured to match the specifications of a particular device model. The goal of this chapter, therefore, is to work through the steps involved in creating such a virtual device using the Pixel 3 phone as a reference example.

4.1 About Android Virtual Devices

AVDs are essentially emulators that allow Android applications to be tested without the necessity to install the application on a physical Android based device. An AVD may be configured to emulate a variety of hardware features including options such as screen size, memory capacity and the presence or otherwise of features such as a camera, GPS navigation support or an accelerometer. As part of the standard Android Studio installation, a number of emulator templates are installed allowing AVDs to be configured for a range of different devices. Custom configurations may be created to match any physical Android device by specifying properties such as processor type, memory capacity and the size and pixel density of the screen.

An AVD session can appear either as separate a window or embedded within the Android Studio window. Figure 4-1, for example, shows an AVD session configured to emulate the Google Pixel 3 model.

New AVDs are created and managed...

4.2 Creating a New AVD

In order to test the behavior of an application in the absence of a physical device, it will be necessary to create an AVD for a specific Android device configuration.

To create a new AVD, the first step is to launch the AVD Manager. This can be achieved from within the Android Studio environment by selecting the Tools -> AVD Manager menu option from within the main window.

Once launched, the tool will appear as outlined in Figure 4-2 if no existing AVD instances have been created:

Figure 4-2

To add an additional AVD, begin by clicking on the Create Virtual Device button in order to invoke the Virtual Device Configuration dialog:

Figure 4-3

Within the dialog, perform the following steps to create a Pixel 4 compatible emulator:

1. From the Category panel, select the Phone option to display the list of available Android phone AVD templates.

2. Select the Pixel 4 device option and click Next.

3. On the System Image screen...

4.3 Starting the Emulator

To perform a test run of the newly created AVD emulator, simply select the emulator from the AVD Manager and click on the launch button (the triangle in the Actions column). The emulator will appear in a new window and begin the startup process. The amount of time it takes for the emulator to start will depend on the configuration of both the AVD and the system on which it is running.

Although the emulator probably defaulted to appearing in portrait orientation, this and other default options can be changed. Within the AVD Manager, select the new Pixel 4 entry and click on the pencil icon in the Actions column of the device row. In the configuration screen locate the Startup orientation section and change the orientation setting. Exit and restart the emulator session to see this change take effect. More details on the emulator are covered in the next chapter (“Using and Configuring the Android Studio AVD Emulator”).

To save time in the...

4.4 Running the Application in the AVD

With an AVD emulator configured, the example AndroidSample application created in the earlier chapter now can be compiled and run. With the AndroidSample project loaded into Android Studio, make sure that the newly created Pixel 4 AVD is displayed in the device menu (marked A in Figure 4-4 below), then either click on the run button represented by a green triangle (B), select the Run -> Run ‘app’ menu option or use the Ctrl-R keyboard shortcut:

Figure 4-4

The device menu (A) may be used to select a different AVD instance or physical device as the run target, and also to run the app on multiple devices. The menu also provides access to the AVD Manager and device connection trouble shooting options:

Figure 4-5

Once the application is installed and running, the user interface for the first fragment will appear within the emulator:

Figure 4-6

In the event that the activity does not automatically launch...

4.5 Running on Multiple Devices

The run menu shown in Figure 4-5 above includes an option to run the app on multiple emulators and devices in parallel. When selected, this option displays the dialog shown in Figure 4-8 providing a list of both the AVDs configured on the system and any attached physical devices. Enable the checkboxes next to the emulators or devices to be targeted before clicking on the Run button:

Figure 4-8

After the Run button is clicked, Android Studio will launch the app on the selected emulators and devices.

4.6 Stopping a Running Application

To stop a running application, simply click on the stop button located in the main toolbar as shown in Figure 4-9:

Figure 4-9

An app may also be terminated using the Run tool window. Begin by displaying the Run tool window using the window bar button that becomes available when the app is running. Once the Run tool window appears, click the stop button highlighted in Figure 4-10 below:

Figure 4-10

4.7 Supporting Dark Theme

Android 10 introduced the much awaited dark theme, support for which is not enabled by default in Android Studio app projects. To test dark theme in the AVD emulator, open the Settings app within the running Android instance in the emulator. There are a number of different ways to access the settings app. The quickest is to display the home screen and then click and drag upwards from the bottom of the screen (just below the search bar). This will display all of the apps installed on the device, one of which will be the Settings app.

Within the Settings app, choose the Display category and enable the Dark Theme option as shown in Figure 4-11 so that the screen background turns black:

Figure 4-11

With dark theme enabled, run the AndroidSample app and note that it appears using a dark theme including a black background and a purple background color on the button as shown in Figure 4-12:

Figure 4-12

The themes used by the light and dark...

4.8 Running the Emulator in a Tool Window

So far in this chapter we have only used the emulator as a standalone window. The emulator may also be run as a tool window embedded within the main Android Studio window. To embed the emulator, select the File -> Settings... menu option (Android Studio -> Preferences... on macOS), navigate to Tools -> Emulator in the left-hand navigation panel of the preferences dialog, and enable the Launch in a tool window option:

Figure 4-14

With the option enabled, click the Apply button followed by OK to commit the change, then exit the standalone emulator session.

Run the sample app once again, at which point the emulator will appear within the Android Studio window as shown in Figure 4-15:

Figure 4-15

To hide and show the emulator tool window, click on the Emulator tool window button (marked A above). Click on the “x” close button next to the tab (B) to exit the emulator. The emulator tool window can accommodate...

4.9 AVD Command-line Creation

As previously discussed, in addition to the graphical user interface it is also possible to create a new AVD directly from the command-line. This is achieved using the avdmanager tool in conjunction with some command-line options. Once initiated, the tool will prompt for additional information before creating the new AVD.

The avdmanager tool requires access to the Java Runtime Environment (JRE) in order to run. If, when attempting run avdmanager, an error message appears indicating that the ‘java’ command cannot be found, the command prompt or terminal window within which you are running the command can be configured to use the OpenJDK environment bundled with Android Studio. Begin by identifying the location of the OpenJDK JRE as follows:

1. Launch Android Studio and open the AndroidSample project created earlier in the book.

2. Select the File -> Project Structure... menu option.

3. Copy the path contained within the JDK...

4.10 Android Virtual Device Configuration Files

By default, the files associated with an AVD are stored in the .android/avd sub-directory of the user’s home directory, the structure of which is as follows (where <avd name> is replaced by the name assigned to the AVD):

<avd name>.avd/config.ini

<avd name>.avd/userdata.img

<avd name>.ini

The config.ini file contains the device configuration settings such as display dimensions and memory specified during the AVD creation process. These settings may be changed directly within the configuration file and will be adopted by the AVD when it is next invoked.

The <avd name>.ini file contains a reference to the target Android SDK and the path to the AVD files. Note that a change to the image.sysdir value in the config.ini file will also need to be reflected in the target value of this file.

4.11 Moving and Renaming an Android Virtual Device

The current name or the location of the AVD files may be altered from the command line using the avdmanager tool’s move avd argument. For example, to rename an AVD named Nexus9 to Nexus9B, the following command may be executed:

avdmanager move avd -n Nexus9 -r Nexus9B

To physically relocate the files associated with the AVD, the following command syntax should be used:

avdmanager move avd -n <avd name> -p <path to new location>

For example, to move an AVD from its current file system location to /tmp/Nexus9Test:

avdmanager move avd -n Nexus9 -p /tmp/Nexus9Test

Note that the destination directory must not already exist prior to executing the command to move an AVD.

4.12 Summary

A typical application development process follows a cycle of coding, compiling and running in a test environment. Android applications may be tested on either a physical Android device or using an Android Virtual Device (AVD) emulator. AVDs are created and managed using the Android AVD Manager tool which may be used either as a command line tool or using a graphical user interface. When creating an AVD to simulate a specific Android device model it is important that the virtual device be configured with a hardware specification that matches that of the physical device.

The AVD emulator session may be displayed as a standalone window or embedded into the main Android Studio user interface.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.1 Development Essentials – Java Edition
Published in: May 2021 Publisher: Packt ISBN-13: 9781801815161
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 €14.99/month. Cancel anytime}