Android Programming Tutorials

(Romina) #1
Sitting in the Background

This requires a graphic image in LunchList/res/drawable/run.png – find


something that you can use that is around 32px high.


Since the menu item is in the menu XML, we do not need to do anything


special to display the item – it will just be added to the menu automatically.


We do, however, need to arrange to do something useful when the menu


choice is chosen. So, update onOptionsItemSelected() in LunchList to look


like the following:


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.toast) {
String message="No restaurant selected";

if (current!=null) {
message=current.getNotes();
}

Toast.makeText(this, message, Toast.LENGTH_LONG).show();

return(true);
}
else if (item.getItemId()==R.id.run) {
new Thread(longTask).start();
}

return(super.onOptionsItemSelected(item));
}

You are welcome to recompile, reinstall, and run the application. However,


since our background thread does not do anything visible at the moment,


all you will see that is different is the new menu item:


72
Free download pdf