Book Image

Mastering Android Studio 3

By : Kyle Mew
Book Image

Mastering Android Studio 3

By: Kyle Mew

Overview of this book

Android Studio is an Integrated Development Environment (IDE) designed for developing Android apps. As with most development processes, Android keeps resources and logic nicely separated, and so this book covers the management of imagery and other resources, and the development and testing tools provided by the IDE. After introducing the software, the book moves straight into UI development using the sophisticated, WYSIWYG layout editor and XML code to design and test complex interfaces for a wide variety of screen configurations. With activity design covered, the book continues to guide the reader through application logic development, exploring the latest APIs provided by the SDK. Each topic will be demonstrated by working code samples that can be run on a device or emulator. One of Android Studio's greatest features is the large number of third-party plugins available for it, and throughout the book we will be exploring the most useful of these, along with samples and libraries that can be found on GitHub. The final module of the book deals with the final stages of development: building and distribution. The book concludes by taking the reader through the registration and publication processes required by Google. By the time you have finished the book, you will be able to build faster, smoother, and error-free Android applications, in less time and with fewer complications than you ever thought possible.
Table of Contents (10 chapters)

Workspace structure

The overall structure of Android Studio is not dissimilar to other IDEs. There are windows for editing text and screen components, others for navigating project structures, and others still for monitoring and debugging. The IDE is highly flexible and can be configured to suit many specific needs and preferences. A typical layout might look like this:

A typical workspace layout

Although these windows can be arranged in any way we please, generally speaking, in the previous screenshot, the four panes might have the following functions:

  1. Navigating a project, module, or library
  2. Editing text and designing layouts
  3. Defining component properties or screen previews
  4. Monitoring and debugging
There are times when a large number of open panes can be distracting; for these times, Studio has a Distraction Free Mode, which displays only the current editor window and can be entered from the View menu.

There are many different perspectives we can take on our projects and many ways to organize them. The best way to see how is to take a look at each in turn.

Editor windows

Naturally, the most important of all the windows in an IDE are those where we create and modify the code that underlies all our apps. Not only do we use editors for our XML and Java, there are, among others, editors for simplifying other resources, such as translations and themes. However graphical the editor may be, all Android resources end up as XML files in the res directory.

It is quite possible to create most Android resources without ever having to write any code at all. Themes can be created with the corresponding editor with nothing more than a few clicks of a mouse. Nevertheless, if we are to consider ourselves as experts, it is important that we have a good understanding of the underlying code and how and where Studio stores these resources. The following example demonstrates how to create a new Android theme using the theme editor:

  1. Start or open an Android Studio project.
  2. Open the theme editor from Tools | Android | Theme Editor.
The theme editor
  1. From the Theme drop-down in the top right corner of the editor, select Create New Theme and enter a name in the New Theme dialog.
  2. Leave the Theme parent field as-is.
  3. Click on the colorPrimary thumbnail.
  4. Choose a color you like from the resultant swatch with a weight of 500.
  5. In the same manner, select the same color with a weight of 700 for the secondary color.
  6. Select a color with a weight of 100 that contrasts nicely with your primary colors for the accent.
  7. Open a preview or the design editor to view these changes.

In the preceding example, we created a new theme that will be automatically applied throughout the application. We could have simply edited the default AppTheme, but this approach will simplify matters if we later decide to employ more than one theme. The IDE applies these changes straightaway by adding something like the following line to the res/values/styles.xml file:

<style name="MyTheme" parent="AppTheme" /> 

The actual color changes can be found in the res/values/colors.xml file.

The theme editor demonstrates rather nicely how Studio editors can create and modify code after little more than a few mouse clicks from us.

All editors can be maximized with Ctrl + Shift +F12. Use the same keys to return to your original layout.

It is also possible to change the theme of the IDE itself by selecting Settings | Editor | Colors and Fonts from the File menu, as displayed in the following image:

The Studio theme dialog

Android Studio comes equipped with just one alternative color scheme, Darcula. This theme presents light text on a dark background and, as such, is far easier on the eye than the default settings, especially for those long, late night development sessions. There are other schemes available online and it can be a lot of fun to design one's own. However, for the purposes of producing printed material, we will stick with the default IDE theme here.

Another good example of a subsidiary editor is the Translations editor, which is also a good way to demonstrate how the project structure differs from other IDEs. The following steps show how this is achieved:

  1. Open the Translations editor by right-clicking on the res/values/strings.xml file and selecting it from the menu. This can also be found from the Language drop-down in the design XML editor.
  2. Click on the globe icon near the top left corner of the editor and select a language from the list.

  1. Select the string you want to translate in the top pane and enter the value in the lower pane, as shown:
The Translations editor

This is a remarkably simple exercise, the point of it being to demonstrate how Android Studio stores such resources and how it displays them. The editor has created a new strings.xml file, identical to our original in every way apart from the string values of our translated text. This file will be referred to automatically by any device that has that language set as the default by the user.

Judging by the Project Explorer, one might think that there was a project directory called strings.xml in the values directory and that it contained two strings.xml files. This, in fact, is presented this way only to help us organize our resources. An examination of the project folder on disk will show that there are in fact two (or more) folders inside the res directory named values and values-fr. Not only does this help organize our work but it also helps minimize the space our apps take up on a device, as only the resource folders that are needed are installed on an end device.

The actual folder hierarchy can always be determined from the navigation bar directly under the main toolbar.
The navigation bar

Themes and Translations are two of the least significant editors but make a good introduction to how Android Studio manages app resources. The majority of a developer's time is spent using code editors and these will, of course, be covered in depth throughout the book. However, although editors make up the core of the IDE, there are many other useful, and even vital, tools available to us, and the most commonly used of these are available from the tools margin.

Tool windows

There are at least a dozen tool windows available to us, more if you have installed plugins. They can be accessed via the View | Tools Windows menu, the tools icon on the far left-hand side of the status bar at the bottom of the workspace, or by pressing Alt and the corresponding number key to open a specific tool window.

The Tool Windows menu

Tool Windows are highly configurable, and each window can be set as docked, floating, or contained in its own window.

The Tool Windows icon on the status bar can be used to hide and reveal tool window tabs around the border of the workspace.

This is particularly useful when working with more than one screen, as follows:

A docked, floating, and windowed tool window

We will be covering all these tools in depth throughout the course of the book. For now though, the following is a brief introduction to the most commonly used tools:

  • Messages: Alt + 0. This tool produces a paired-down version of the Gradle build process. A more detailed output can be found within the Gradle Console.
  • Project: Alt + 1. Usually docked to the left of the workspace, this tool is our main navigational tool.
  • Favorites: Alt + 2. This is a very handy organizational tool, providing quick access to commonly used classes and components. To add any file to the Favorites list, simply right-click on it in the project window and select Add to Favorites from the drop-down menu.
  • Run: Alt + 3. A powerful diagnostic tool that becomes available when an application is running on a device or emulator.
  • Android: Alt + 4. This is Studio's main debugging window and is used to monitor log output from a running application and take screenshots.
  • Memory Monitor: Alt + 5. This incredibly useful tool produces a live graph of memory usage as an application is running.
  • Structure: Alt + 6. This tool produces detailed information about the current editor, showing a hierarchical view of classes, variables, and other components contained in that particular file.

One of the most useful Tool Windows is the Device File Explorer tool. This allows us to browse the filesystem of any connected device or emulator.

The Device File Explorer tool.
All application files can be found in data/data.

Tool windows are fantastically useful and enable us to configure the IDE to suit the particular task we are working on. Being able to select appropriate tools like this is one of Android Studio's most useful features. Of course, Android Studio is nothing more than a frontend interface that allows us to connect with the real power behind Android, the SDK.