Preference Settings

Preference Settings

Android Preference Settings icon

Preference Settings allow the user to select basic settings for an app. It’s a way of customising the app to suite the user.

This tutorial will show you how you can create your own custom Preference Settings for your apps.

We’ll be using fragments and Headers so this tutorial is suitable for apps developed for devices running Android 3.0 and up.

There are four parts to the Preference Framework. We’ll cover all of them:

  • Preference Screen Layout –the xml file defining your settings items
  • Preference Activity and Fragment – these host the Preference screens. The activity hosts the fragment and the fragment hosts the Preference Screen, displaying your settings
  • Preference Headers – these are lists of subscreens. An xml file defines the Preference Fragments used for the Headers subscreens
  • Shared Preference Change Listener – listens for any changes in the Shared Preference values

Each Preference appears as an item in a list. The user is able to modify these preference settings.

Contextual menus, action mode, Contextual Action Bars and other stuff!

Android Contextual Menus icon

So what’s this all about?

Well, it’s sort of about context menus but different.

It’s all about using the contextual action mode. This lets the user do stuff with the items that they have selected.

Enabling the contextual action mode, displays a Contextual Action Bar at the top of the screen. You can then place action items, which are like menu options, on this bar.

The user selects items from a list for example, chooses an action item and the action linked to that action item is performed on all of the selected items.

Here’s an example

Say you had a list of countries and you want the user to be able to select a couple of them to add to a database. This is where you could use the contextual action mode.

It enables the user to select one or more of the countries in the list. They can then select a Save action item, for example, and all the selected country items will be saved in the database.

Menus. How to use them in your apps

Android menus

What you’ll learn

We’ll show you how to:

  • Create an options menu
  • Place Action items on the Action Bar
  • Create a Context menu
  • Create a Popup menu
  • Use the support library so that you can display Action Bars and popup menus on earlier versions of Android

Here’s how to use Broadcast Receivers in your apps: A Tutorial

Broadcast receivers tutorial icon

What you’ll learn

This tutorial will show you how to:

  • Register a Broadcast Receiver in your app’s manifest file. We’ll use this receiver to receive a System broadcast sent when the power cable is plugged into the device
  • Register a Broadcast Receiver in an activity. We’ll use this one to show you how to use permissions to secure your broadcasts and the receiver. This receiver will only receive broadcast intents that have the required permission
  • Use the LocalBroadcastManager to send and receive a local broadcast. A secure way of using broadcasts within your app

Android Broadcast Receivers

Broadcast Receivers: getting to know them

Android broadcast receivers tutorial icon

You’ve probably used intents to start activities. You can also use intents to broadcast messages.

The broadcast intent carries information about who should receive the message and what should be done with it.

Broadcast Receivers can receive these messages.

This broadcast mechanism runs in the background. The user is not aware of it as Broadcast receivers don’t have a User Interface.

You can also use Broadcast receivers to do small bits of work which must complete within 5 seconds. If they don’t, then an Application Not Responding dialog appears.

Usually you would use a receiver to start other components like Services. You can also send a notification from the receiver.

Android Activities

Getting active: all about Activities

Android Activity icon

Activities are application components that we, mostly, see as the screen. It displays images, text, buttons, etc. that we interact with. Some activities don’t display anything.

Apps usually have a number of activities. One is seen as the main activity. This is the first screen we see when the app starts.

Using your application resources

Application Resources icon

You should keep your resources, like images, strings, values, etc. separate from your code.

Application Resources folders

Some of the folders in the res directory

Bound Services. A way to interact with the Service

Bound Service tutorial icon

Our tutorial app uses a Service to play a sound clip.

We start a Service when the app starts. The Service will run as long as the app is alive.

We need to bind to the Service to start and stop playing a sound clip. We can leave the clip playing in the Service while it is running. It will continue to play even if the activity is paused or destroyed.

Bound Services: a primer

Bound Services primer icon

Quick recap on Services

  • Services are app components that you can use to do work in the background.
  • Services run in your app’s process in the main thread. Don’t block the main thread. Use a separate thread to do any heavy work in your Service.
  • You can also use Services running in another app’s process.
  • Services continue to run until you stop them. The system can also kill them at any time but you can set their priority so that they are unlikely to be killed.

So what happens when we bind to a Service?

Out in the open: The foreground Service

Foreground Service Tutorial icon

It’s unlikely that the system will kill a foreground Service.

Typically we’d use foreground Services for work that the user is aware of, like playing music.

When we use a foreground Service, we have to send a notification to:

  • let the user know that the Service is running
  • give the user the opportunity to stop it

If you haven’t already done so, you may want to have a look at the article All about Services and Part 1 of the series of Services tutorials, A Simple Service