Drawer Navigation

Using Navigation Drawers in your apps

Drawer Navigation drawer icon

The Navigation Drawer is a panel where you can put your navigation options. The panel is hidden off-screen and displays when the user swipes it onto the screen.

You can also use the app icon as a button to open and close the drawer.

Swipe View Tutorial

Using Swipe Views to swop pages

Android Swipe Views icon

Swipe views allow you to change pages by swiping your finger across the screen.

You create Swipe views by using a page viewer and an adapter. The adapter supplies the pages and the page viewer takes care of moving the pages on and off the screen.

We’re using a ViewPager. It’s a layout manager that takes care of the horizontal scrolling. Usually it’s used with fragments, scrolling them on and off screen.

We’re also going to need a page adapter to supply the pages to the ViewPager.

As we’re using fragments, we have two adapter options:

  • FragmentPagerAdapter – represents each page as a fragment. The fragment is kept in the fragment manager while it’s still possible that it may be used. This adapter is best used for a small number of fragments. It can use lot of memory as fragments are kept in memory while not displayed
  • FragmentStatePagerAdapter – uses fragments to manage the pages. It also saves and restores the fragment’s state. It’s useful for a large number of pages as it uses less memory. Only the state of the fragment is kept in memory and not the whole fragment. It may be a bit slow switching between pages due to it first having to restore their state

We’re going to use the FragmentStatePagerAdapter.

Tab navigation tutorial

Using Tab navigation in your apps

Android Tabs navigation icon

Tab buttons are displayed in the Action Bar. You can also use images instead of buttons or even your own custom views.

Tabs are designed to work with fragments. Use them to swop fragments in your activities.

We’ll show you how to use Tabs to swop fragments in your activity.

Using fragments to rebuild your activities

Android configuration change and ragments icon

An activity is destroyed and recreated when a device’s configuration changes.

Usually the activity’s lifecycle takes care of saving and restoring its state. Sometimes you may need to restore lots of data. This could take some time resulting in a bad user experience.

Here’s where you can use fragments to retain an instance of your data. You can then get the data out of the fragment and rebuild your activity, super fast!

Be careful not to save objects that are tied to the activity; such as drawables, adapters, views or any other objects associated with a context. They’ll cause memory leaks – the object keeps a reference to the context (the activity for example), so it will remain in memory, leaving less for your app to use.

Have a look how easy it is to use a fragment to save an Object during a configuration change!

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

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