You're reading from Android 9 Development Cookbook - Third Edition
With an understanding of Fragments from the previous chapter, we're ready to expand on our discussion of widgets. In Chapter 3, Views, Widgets, and Styles, we discussed how to add widgets to your own app. Now, we'll look at how to create an App Widget so users can add your app on their Home screen.
The remaining recipes in this chapter will explore System UI options. There's a recipe for adding a Search option to the Action Bar using the Android SearchManager API. Another recipe will explore Full Screen mode and several additional variations on altering the System UI. The final recipe will showcase the new Lock Screen shortcuts introduced in Android O (API 26).
This recipe explains how to create a link or create a shortcut for your app on the user's Home screen. So as not to be too obtrusive, it's generally best to make this an option for the user to initiate, such as in the settings.
The following is a screenshot showing our shortcut on the Home screen:
As you can see, this is just a shortcut to your app. The next recipe will go deeper by creating a Home screen (AppWidget).
Create a new project in Android Studio and call it HomeScreenShortcut
. Use the default Phone & Tablet options and select the Empty Activity
option when prompted for the Activity Type.
For an app to create a shortcut, it must have the INSTALL_SHORTCUT
permission. With the appropriate permission, it's a simple matter of calling an intent with your app properties. The following are the steps:
- Open the
AndroidManifest
file and add the following permission:
<uses-permission android:name="com.android.launcher.permission...
Before we dig into the code for creating an App Widget, let's cover the basics. There are three required and one optional component:
- The
AppWidgetProviderInfo
file: It's an XML resource (described later) - The
AppWidgetProvider class
: This is a Java class - The
View layout
file: It's a standard layout XML file, with some restrictions (explained later) - The App Widget configuration Activity (optional): This is an Activity the OS will launch when placing the widget to provide configuration options
The AppWidgetProvider
must also be declared in the AndroidManifest
file. Since AppWidgetProvider
is a helper class based on the Broadcast Receiver, it is declared in the manifest with the <receiver>
element. Here is an example manifest entry:
<receiver android:name=".HomescreenWidgetProvider" > <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android...
Along with the Action Bar, Android 3.0 introduced the SearchView widget, which can be included as a menu item when creating a menu. This is now the recommended UI pattern to provide a consistent user experience.
The following screenshot shows the initial appearance of the Search icon in the Action Bar:
The following screenshot shows how the Search option expands when pressed:
If you want to add Search functionality to your application, this recipe will walk you through the steps to set up your User Interface and properly configure the Search Manager API.
Create a new project in Android Studio and call it SearchView
. Use the default Phone & Tablet options and select Empty Activity
when prompted for the Activity Type.
Android 4.4 (API 19) introduced a UI feature called Immersive Mode. Unlike the previous full-screen flag, your app receives all touch events while in Immersive Mode. This mode is ideal for certain activities, such as reading books and news, full-screen drawing, gaming, or watching a video. There are several different approaches to full-screen, and each has a best use case:
- Reading books/articles, and so on: Immersive Mode with easy access to the System UI
- Game/drawing app: Immersive Mode for full-screen use but minimal System UI
- Watching video: Full-screen and normal System UI
The key difference between the modes is how the System UI responds. In the first two scenarios, your app is expecting user interaction, so the System UI is hidden to make it easier for your user (such as not hitting the back button while playing a game). While using full-screen with a normal System UI, such as watching a video, you wouldn't expect your user to use the screen at all, so when...