Add navigation style to your Android mobile app The Activity class is the foundation of Android mobile applications, and you can use it to optimize the interaction of your application with users and mobile devices. Make the interactions in the application life cycle exactly the way you expect, and use icons and action bars to guide users to use UI navigation and other application functions. Introduction About this series Mobile application releases have exploded in recent years, and so has the market for mobile development technologies. This series of articles will introduce the evolution of mobile applications to developers who are familiar with programming but new to mobile technologies. It starts with coding native applications using Java™ code, and then expands the toolkit to include JVM languages, scripting frameworks, HTML5/CSS/JavaScript, third-party tools, and more. This series of articles will take you step by step to master these necessary technologies to handle all mobile development scenarios.
Today's mobile devices are incredibly powerful, far more powerful than the desktop computers that many developers used to write their first programs. As a result, it's easy to forget that mobile devices are still resource-constrained environments. When developing mobile applications, it's important to remember the limitations of the environment in which your application will run. This is especially true when your application is competing for system resources with other applications—some of which are more important to the user's daily behavior than your application. One way to ensure your app is popular is to make sure it conserves system resources. In Android, the mechanism for using and maintaining system resources is We'll start with a quick look at Activity class life cycle The life cycle of Figure 1. Android Activity life cycle The Android mobile application life cycle consists of four stages:
The following content will describe each stage and its callback method (which can be implemented inside Startup in the Activity life cycle Demo Application If you have been following this series, you have already created your own demo application in the first and second articles in this series. If you do not have a demo application, I recommend that you create one before continuing. Alternatively, you can clone the Git repository of my own Overheard Word demo application. In the previous article, you have used the callback method corresponding to starting In the Eclipse Android development environment, you can easily override methods by selecting the Override/Implement Methods... option, as shown in Figure 2. Figure 2. Overriding Activity lifecycle callback methods Next, select Figure 3. Select callback Now add some tracing statements using Android's Listing 1. Implementing the Android Activity callback
Check the results by starting an instance of the application and viewing the logs through LogCat, as shown in Figure 4. Figure 4. Debug statements in LogCat Android uses LogCat to record logs Android has its own logging system, As you've probably guessed, Pause and resume in the Activity life cycle Because mobile devices often run multiple applications that compete for the user's attention in various ways, your application should know when to let another application occupy the device screen and use more resources. Sometimes, the user needs to answer a phone call while using your application, or the application may pop up a dialog box, such as an information request or an error message. Each of these actions will partially block the current When an When the application is completely hidden, such as when the user makes a phone call, Here's what happens when you implement Destroying In Listing 2, I demonstrate this process using my Overheard Word application by calling the Listing 2. Destroying an Activity instance
The most commonly used It may not be obvious now, but once you start working with external resources—such as an external Web service or a device's file system or database—these lifecycle phases become very important. Next, we will explain how to use two Navigating using menus and actions When I created the Overheard Word project in Eclipse, the first In the simulator instance that represents the old device, there is a button called "Menu." When you click it, the application instance displays a menu of options. In this example, we'll see options for navigation. For example, if the user presses the Home button, you'll see something like Figure 5. Figure 5. An unimplemented menu item There is no menu button on tablets. Instead of selecting items from a menu , users are required to initiate various actions . This newer UI bar is called the action bar , as shown in Figure 6. Figure 6. Android's new action bar Although the menu button behaves in a similar way to the action bar, the action bar is only implemented on newer devices. Since we are targeting older versions of Android (remember, about 50% of Android devices run Gingerbread!), I will demonstrate using the more familiar menu button. Later, I will explain how to update the navigation code to implement the action bar if you want to target newer versions of Android and corresponding devices. Creating an options menu In order to refurbish Overheard Word to improve the efficiency of user interaction, the first step is to implement an options menu so that users can exit the application. Exiting the application is one of the stages of Remember, in an Android application, all UI-related business corresponds to an XML file, so the way to define the UI is to edit the layout XML file. The XML files of an Android application are located in a specific directory in the For a quick exercise, take a look at the default implementation of
If you're thinking about looking for an XML file called I... quit! Next, we will edit the menu resource XML file and add a menu item called <string name="quit_menu">Quit</string> This tag defines the word Quit , which can be referenced by the identifier Now, start the simulator and press the Menu button. You should see a menu appear at the bottom of the screen with one option: Quit . But selecting it will have no effect, because it is not yet implemented. We'll add the implementation code for the Quit option in a minute. But first, let's consider another important element of any functional part of a mobile app, its appearance. You may have noticed that a lot of mobile UIs today (and even more and more web app UIs) use icons for navigation. Here's how to replace generic word buttons with free icons. Icons in Mobile UI Design Before I got into mobile development, I dabbled in icons, but rarely used them in my enterprise applications. When web applications started to become more interactive, I found myself using icons more often. But it wasn't until I started working in mobile development that icons really became a focus of my work. Where is my icon? It's easy to find free icons for use in open source and commercial applications these days. You can also pay a small fee to purchase the rights to use certain icons. I personally like a package called Glyphish that has hundreds of icons to choose from, and the licensing fee is very reasonable. Glyphish also offers a free license. I recommend that you quickly search and find the icons you want to use for the demonstrations in this article. If you use icons in your Android mobile UI design, you need to be fully aware of the device resolution. The Android device ecosystem is huge, and your application may need to run on a variety of devices, from low-resolution small-screen devices to high-resolution tablets with large 7-inch screens. An icon that looks good on a handheld device may look very rough on a tablet. Fortunately, you can control how your app icons look on different devices. Take a quick look at the For example, for devices with very high resolutions, Android will use icons from the To keep things simple, I will create a Figure 7. Adding an icon to the drawable directory My next step was to reference the icon in the options menu by updating
If you followed my steps closely, you should update
It's helpful to use a descriptive, understandable string value when we move on to the next step, implementing the quit behavior inside Figure 8. Nice icons! (Provided by Glyphish) Implementing menu behavior Now I have a nice-looking icon that I can use for the Quit menu item (and I hope you have an icon for your own applications, too), but I still need to add the code that tells the application what to do when the button is pressed. Implementing any behavior in the options menu starts with overriding the Listing 3. Handling menu item selection
Note that this is just a simple Try this new code in the simulator: press the Menu button, choose the Exit ( X ) option, and watch what appears in LogCat. You should see a complete Action Bar in Android 3.x As mentioned earlier, newer versions of Android (Honeycomb and later) use an action bar instead of an options menu. Newer devices may not even have a Menu button, so understanding the app's navigation (or other functionality) can also be done in the action bar. It doesn't take a lot of work to ensure that the navigation functionality you implemented by coding for the options menu also works with the action bar. You have already implemented all the required methods, and the only work left is to make some changes to the XML source file. First you need to create an emulator instance that simulates a device that uses an action bar instead of a menu button. The simplest way to do this is to emulate a tablet. Start the Android SDK Manager command-line application (the Figure 9. Creating a tablet emulator Next, launch your app in the tablet instance. You should see a vertical line of three dots in the lower right corner. It looks great, right? By default, Android preserves the menu behavior even on newer display devices. You can upgrade the look and behavior of the action bar to be more natural by updating your app's XML resources. Start with your app's
Next, go to your project's Properties page in Eclipse and update the Project Build Target to any Android version higher than Android 4.2.2. Click OK and let the project rebuild. Then find the menu XML file in
Finally, if your project does not already have two subdirectories named
In
Now restart the simulator and your new icon should appear in the top right corner: Figure 10. An action bar with icons Now go back to the menu file in Don't forget to reset Note that if you want to keep your application targeting Gingerbread, you will need to re-target your project and undo the changes you made to the XML files in this section. These changes are not backwards compatible! More fun with icons So far, you've added a menu option to an application targeting Gingerbread, seen it translate beautifully to new devices that implement the action bar, and learned how to upgrade that functionality with a few updates to the application's XML files if you choose to do so. Now, let's summarize what you've learned about icons. You've already done the main work of adding an icon for your app's navigation menu, so updating its main icon shouldn't be a problem. This icon represents your app to users, so it's important to know how to update and customize it. I'm going to update Overheard Word's icons, and you can do the same for your own applications. Again, a quick search on the Internet will yield a plethora of icon sites. Among these sites, I found an interesting collection of icons, and one that I really liked: Figure 11. A beautiful new icon
Recall that this icon renders differently on different device profiles. I wanted to ensure that users had a good first impression of Overheard Word, so it was imperative that the icon resolution be appropriate for the range of devices I was targeting. Luckily, I knew of a really great site that could help me achieve this goal. Investment Icon There's nothing wrong with using free icons for demonstration purposes, but investing in custom icons for a professional app is absolutely necessary. Your icon (or icon set) represents your app's brand, and you want it to be unique. If it's generic, or looks decidedly amateurish, what conclusions will users draw about your app? Android Asset Studio is a Google Code project that has a ton of useful tools to help Android developers. One tool I use a lot is Launcher Icons. All I have to do is click on the Launcher Icons link and upload my icon. The utility generates the correct sized icon files for various device profiles, which I can then download as a zip file. The file contains four directories, each containing a specific resolution and size version of the file I uploaded. Figure 12. Launcher Icons creates correctly sized icons for Android. Next, I copied the Finally, I run the app again and wait for it to appear in my simulator instance. I click the Home button and look at the result: a beautiful app icon that (to me) signals that OverHeard Word is the most interesting app on any user's device! Figure 13. Now it becomes very vivid! Conclusion In this article, you learned about Everything you've learned in this article is essential for building Android applications. Mobile development on Android is easy to pick up and certainly fun, but I want you to understand that it's a different paradigm from the Java development you're familiar with. There are thousands of apps in Google Play and other app stores, so it's usually the well-planned, carefully designed, and cleverly coded ones that make it to the top. There's still a lot to learn! |
<<: Apple Watch UI Animation Analysis
>>: Mobile technology for the masses: words and gestures from the Overheard Word
Training course content: Maybe you spent a lot of...
Ask a question I have come into contact with many...
Growth is a process of continuous experimentation...
Netbooks fueled a PC buying frenzy in 2008, as hu...
recently Public Security Bureau of Dangyang City,...
1. Entrustment Model 1. Usage process The most co...
In recent years, the battle in the mid-size SUV m...
The past 10 years have been the fastest-growing d...
How much does it cost to attract investment for t...
1. According to people familiar with the matter, ...
The word "Begonia" was first seen in th...
01 Market Analysis 1.1 Market Definition There is...
The pirate model is often used in product operati...
iTunes has never been a good tool, and after year...
As of 8:30 a.m. on August 26 Through joint effort...