Reader small image

You're reading from  Android 9 Development Cookbook - Third Edition

Product typeBook
Published inOct 2018
Reading LevelIntermediate
PublisherPackt
ISBN-139781788991216
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Rick Boyer
Rick Boyer
author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer

Right arrow

Chapter 6. Home Screen Widgets, Search, and the System UI

In this chapter, we will cover the following topics:

  • Creating a shortcut on the Home screen
  • Creating a Home screen widget
  • Adding Search to the Action Bar
  • Showing your app full-screen
  • Lock screen shortcuts

Introduction


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).

Creating a shortcut on the Home screen


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).

Getting ready

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.

How to do it...

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:

  1. Open the AndroidManifest file and add the following permission:
<uses-permission android:name="com.android.launcher.permission...

Creating a Home screen widget


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...

Adding Search to the Action Bar


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.

Getting ready

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.

How to do it...

To set up the Search UI pattern, we need to create the Search menu item and a resource called searchable. We'll create a second activity to receive the search query. Then, we'll hook it all up in the AndroidManifest file...

Showing your app full-screen


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...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android 9 Development Cookbook - Third Edition
Published in: Oct 2018Publisher: PacktISBN-13: 9781788991216
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.
undefined
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

Author (1)

author image
Rick Boyer

Rick Boyer has been programming professionally for over 20 years. He has written apps on Windows, created websites, and coded for various mobile devices, including Windows CE, Windows Phone, and Android. Almost eight years ago, he took the plunge and started his own software consulting business, NightSky Development, focusing exclusively on Android development.
Read more about Rick Boyer