In the previous section, menu options are not text. If you want to display both icons and text together, you can use the "|" and Menuitem.show_as_action_with_text constants.
MenuItem mnu1 = menu.add (0, 0, 0, "Item 1");
{
Mnu1.seticon (r.drawable.ic_launcher);
Mnu1.setshowasaction (
menuitem.show_as_action_if_room |
Menuitem.show_as_action_with_text);
}
In addition to clicking on the action item, you can also click on the application icon Actionbar above. When the application icon is clicked, the Onoptionsitemselected () method is invoked. If you want to identify the application icon is clicked, you can use Adnroid. R.id.home constants.
Private Boolean Menuchoice (MenuItem Item)
{
switch (Item.getitemid ()) {
case Android. R.id.home:
toast.maketext (This,
"your clicked on the application icon",
Toast.length_long). Show ();
return true;
Case 0:
toast.maketext (This, "Your clicked on Item 1",
Toast.length_long). Show ();
return true;
Case 1: ...
}
If you want the application icon to be clicked, we need to call the Setdisplayhomeasupenable () method:
/** called the activity is a. * *
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Actionbar Actionbar = Getactionbar ();
Actionbar.setdisplayhomeasupenabled (true);
Actionbar.hide ();
Actionbar.show (); ---show it again---
}
When clicking the Apply icon:
In general, an application may contain many activity, and an application icon is usually used to return to the main activity. If you want to do this, you need a intent object and use the INTENT.FLAG_ACTIVITY_CLEAR_TOP flag.
Case
Android. R.id.home:
toast.maketext (This,
"your clicked on the application icon",
Toast.length_long). Show ();
Intent i = new Intent (this, myactionbaractivity.class);
I.addflags (intent.flag_activity_clear_top);
StartActivity (i);
return true;
Using the Intent.flag_activity_clear_top identification, you can make sure that when you click on the application icon, the activity in the "Back stack" will be cleared away. With this method, if the user clicks the return key, the other activity will not be displayed again.