Android App Development
Lesson 9 - ActionBar, Toasts, Notifications & Preferences
This Week
● The ActionBar
● Toasts
● Notifications - Notifcation Area and Notification
Drawer
● Preferences
The ActionBar
● As well as displaying the title we can add actions to the
ActionBar.
● A typical well known example would be actions on the
ActionBar of the Android GMail App.
Each action is typically
added to the ActionBar via a
menu resource XML file
within the menu directory of
your app resources.
The ActionBar
● The ActionBar is the part of at the top of an Activity
where we’ve already seen the title of the Activity
NOTE : ActionBar was introduced in
Android 3.0. Before that you needed to
use an additional library. This is why you
sometimes see appcompat.v7 alongside
your project. If you choose just to
support 3.0 and up (which is
reasonable) you won’t need that library.
Just import android.app.ActionBar
from 3.0 on.
Adding ActionBar Actions
● If like GMail we wanted to add two actions to the
ActionBar (e.g 1. Search & 2. Compose EMail) our XML would
look something like this…...
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"/>
<item android:id="@+id/action_compose"
android:icon="@drawable/ic_action_compose"
android:title="@string/action_compose" />
</menu>
Each item on the
action bar is
treated as a
menu item and
has the following
attributes
1. ID
2. Icon
3. Title
To display an icon (if possible)
use
android:showAsAction=”ifRoom”
ActionBar Java Code
● As similar to fragment XML files we need to “inflate”
the menu XML file within the lifecycle of the Activity.
● To do this we override the onCreateOptionsMenu
lifecycle method of Activity
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.mainmenu, menu);
return true;
}
This presumes our menu
resource file is called
mainmenu.xml
NOTE : If there are too
many actions the the
overflow button will
appear.
Reacting to ActionBar Click
● When an item on the ActionBar of an Activity then
the onOptionsItemSelected method of that Activity is
invoked. So we override that method…..
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search://Do the search
break;
case R.id.action_compose://Do the compose
break;
}
return true;
Remember these
were the IDs
defined in the
menu resource
XML file
Toasts
● A Toast in Android is used for very simple feedback
messages in a small (and usually short-lived) popup which
is just large enough to contain the text.
To create a Toast we call a static
method on the Toast class
Toast myToast =
Toast.makeText(...);
which takes the following
parameters
1. Application Context
Toasts
Context context = getApplicationContext();
CharSequence text = "Mail has been sent!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
By default a Toast will appear above the
bottom center of the Activity.
Notifications
● Most Android users rely heavily on the Notifications
area of their device => Consider using it in your app!
● Notifications in Android consists of two distinct areas
The Notification Area
The Notification
Drawer
What’s in a Notification ?
● A Notification object can has many different pieces of
information which can be set but MUST contain at least
the following…..
❏ A Small Icon (for the Notification Area) - setSmallIcon()
❏ A Title - setContentTitle()
❏ Detail Text - setContentText()
The following is also common but non-mandatory..
❏ An Intent to launch something if notification is
clicked- setContentIntent()
Creating a Notification
● Similar to an AlertDialog (previous lecture) we need a
Builder to create a Notification.
//Create the Builder
NotificationCompat.Builder myBuilder =new NotificationCompat.Builder(this)
//Set the values of the Notification
myBuilder.setSmallIcon(R.drawable.my_notification_icon);
myBuilder.setContentTitle(“You have a new message!”);
myBuilder.setContentText(“Hi, how are you ?”)
//”Build” the Notification object
Notification myNotification = myBuilder.build();
Triggering a Notification
● Once a notification is built, it needs to be triggered
using the NotificationManager object.
● NotificationManager is something which is already
instatiated and present in the Android Platform. We
access it as follows …….
NotificationManager nManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Triggering a Notification
● Once we have an instance of both a Notification object
and a NotificationManger we trigger the notification via
the notify() method of NotificationManger.
nManager.notify(0, myNotification);
The
NotificationManager
instance
The Notification
instance created
by the Builder.
This int is the Notification ID. It should
be unique for each notification within
your app. If you trigger another
Notification with the same ID it will
replace the original
Preferences
● In the vast majority of apps there will be a “Settings”
section (and the user will expect it of any decent app)
● In the last lecture we saw the “model” for storing
Preferences (i.e. SharedPreferences)
● Now we’ll focus on the “view” part of Preferences
which we will build by subclassing two Android UI
classes.
PreferenceActivity - For our preference Activity!
Preference - For each item in the Preference
Activity.
Preferences UI
● PreferenceActivity extends ListActivity (which we’ve
already mentioned) (Note : There’s also a PreferenceFragment)
● Preference already has a number of useful subclasses
defined (e.g. CheckBoxPreference, ListPreference &
EditTextPreference)
● As with any Android interface we have done thus far
we add our UI components via XML.
● For preferences it is /res/xml/preferences.xml
Preferences XML
● For the moment we’ll look at how we’d add a
CheckboxPreference
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
android:key="pref_auto_refresh"
android:title="@string/pref_autorefresh"
android:summary="@string/pref_autorefresh_summary"
android:defaultValue="true" />
</PreferenceScreen>
● We must have a key and defaultValue which get stored in
SharedPreferences
Bringing it together
● Finally we create our Activity (or Fragment) and read
in the XML file.
public class SettingsActivity extends PreferenceActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
● This automatically creates a SharedPreferences and
saves the preferences there! (OK for basic preference storage)

Lesson 9

  • 1.
    Android App Development Lesson9 - ActionBar, Toasts, Notifications & Preferences
  • 2.
    This Week ● TheActionBar ● Toasts ● Notifications - Notifcation Area and Notification Drawer ● Preferences
  • 3.
    The ActionBar ● Aswell as displaying the title we can add actions to the ActionBar. ● A typical well known example would be actions on the ActionBar of the Android GMail App. Each action is typically added to the ActionBar via a menu resource XML file within the menu directory of your app resources.
  • 4.
    The ActionBar ● TheActionBar is the part of at the top of an Activity where we’ve already seen the title of the Activity NOTE : ActionBar was introduced in Android 3.0. Before that you needed to use an additional library. This is why you sometimes see appcompat.v7 alongside your project. If you choose just to support 3.0 and up (which is reasonable) you won’t need that library. Just import android.app.ActionBar from 3.0 on.
  • 5.
    Adding ActionBar Actions ●If like GMail we wanted to add two actions to the ActionBar (e.g 1. Search & 2. Compose EMail) our XML would look something like this…... <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_search" android:icon="@drawable/ic_action_search" android:title="@string/action_search"/> <item android:id="@+id/action_compose" android:icon="@drawable/ic_action_compose" android:title="@string/action_compose" /> </menu> Each item on the action bar is treated as a menu item and has the following attributes 1. ID 2. Icon 3. Title To display an icon (if possible) use android:showAsAction=”ifRoom”
  • 6.
    ActionBar Java Code ●As similar to fragment XML files we need to “inflate” the menu XML file within the lifecycle of the Activity. ● To do this we override the onCreateOptionsMenu lifecycle method of Activity @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.mainmenu, menu); return true; } This presumes our menu resource file is called mainmenu.xml NOTE : If there are too many actions the the overflow button will appear.
  • 7.
    Reacting to ActionBarClick ● When an item on the ActionBar of an Activity then the onOptionsItemSelected method of that Activity is invoked. So we override that method….. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_search://Do the search break; case R.id.action_compose://Do the compose break; } return true; Remember these were the IDs defined in the menu resource XML file
  • 8.
    Toasts ● A Toastin Android is used for very simple feedback messages in a small (and usually short-lived) popup which is just large enough to contain the text. To create a Toast we call a static method on the Toast class Toast myToast = Toast.makeText(...); which takes the following parameters 1. Application Context
  • 9.
    Toasts Context context =getApplicationContext(); CharSequence text = "Mail has been sent!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); By default a Toast will appear above the bottom center of the Activity.
  • 10.
    Notifications ● Most Androidusers rely heavily on the Notifications area of their device => Consider using it in your app! ● Notifications in Android consists of two distinct areas The Notification Area The Notification Drawer
  • 11.
    What’s in aNotification ? ● A Notification object can has many different pieces of information which can be set but MUST contain at least the following….. ❏ A Small Icon (for the Notification Area) - setSmallIcon() ❏ A Title - setContentTitle() ❏ Detail Text - setContentText() The following is also common but non-mandatory.. ❏ An Intent to launch something if notification is clicked- setContentIntent()
  • 12.
    Creating a Notification ●Similar to an AlertDialog (previous lecture) we need a Builder to create a Notification. //Create the Builder NotificationCompat.Builder myBuilder =new NotificationCompat.Builder(this) //Set the values of the Notification myBuilder.setSmallIcon(R.drawable.my_notification_icon); myBuilder.setContentTitle(“You have a new message!”); myBuilder.setContentText(“Hi, how are you ?”) //”Build” the Notification object Notification myNotification = myBuilder.build();
  • 13.
    Triggering a Notification ●Once a notification is built, it needs to be triggered using the NotificationManager object. ● NotificationManager is something which is already instatiated and present in the Android Platform. We access it as follows ……. NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  • 14.
    Triggering a Notification ●Once we have an instance of both a Notification object and a NotificationManger we trigger the notification via the notify() method of NotificationManger. nManager.notify(0, myNotification); The NotificationManager instance The Notification instance created by the Builder. This int is the Notification ID. It should be unique for each notification within your app. If you trigger another Notification with the same ID it will replace the original
  • 15.
    Preferences ● In thevast majority of apps there will be a “Settings” section (and the user will expect it of any decent app) ● In the last lecture we saw the “model” for storing Preferences (i.e. SharedPreferences) ● Now we’ll focus on the “view” part of Preferences which we will build by subclassing two Android UI classes. PreferenceActivity - For our preference Activity! Preference - For each item in the Preference Activity.
  • 16.
    Preferences UI ● PreferenceActivityextends ListActivity (which we’ve already mentioned) (Note : There’s also a PreferenceFragment) ● Preference already has a number of useful subclasses defined (e.g. CheckBoxPreference, ListPreference & EditTextPreference) ● As with any Android interface we have done thus far we add our UI components via XML. ● For preferences it is /res/xml/preferences.xml
  • 17.
    Preferences XML ● Forthe moment we’ll look at how we’d add a CheckboxPreference <?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <CheckBoxPreference android:key="pref_auto_refresh" android:title="@string/pref_autorefresh" android:summary="@string/pref_autorefresh_summary" android:defaultValue="true" /> </PreferenceScreen> ● We must have a key and defaultValue which get stored in SharedPreferences
  • 18.
    Bringing it together ●Finally we create our Activity (or Fragment) and read in the XML file. public class SettingsActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } } ● This automatically creates a SharedPreferences and saves the preferences there! (OK for basic preference storage)