Use of Android Actionbar

Source: Internet
Author: User
Tags home screen


 action Bar main features include:
  1. Display the Options menu
  2. Provide navigation function for the switch mode of the tab page, Multiple fragment.  can be toggled,
  3.  provides a drop-down navigation entry.
  4. Provides an interactive active view instead of an option entry  
  5. Use the program's icon to return to the home screen or navigate up.


First of all, when using overflow, you need to call the following method in the OnCreate () function:

private void Forceshowoverflowmenu () {      try {          viewconfiguration config = viewconfiguration.get (this);          Field Menukeyfield = Viewconfiguration.class                  . Getdeclaredfield ("Shaspermanentmenukey");          if (Menukeyfield! = null) {              menukeyfield.setaccessible (true);              Menukeyfield.setboolean (config, false);          }      } catch (Exception e) {          e.printstacktrace ();      }  }

Note: This implementation is not used here, and the popupmen used in this application


The following are the Actionbar used by the projects you develop:

public class Shoporderactivity extends Actionbaractivity Implementsontouchlistener, onmenuitemclicklistener{private Toast mtoast;private Context context;/** alertdialog input Feedback box */private EditText et_feedback;private popupmenu popupmenu;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.shop_order);//Set Home icon area//requestwindowfeature (Window.feature_left_icon);//setfeaturedrawableresource (Window.feature_left_icon, resId); this.context = This;mtoast = Toast.maketext (This, "", Toast.length_long); Initactionbar ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoIdManifest.xml.int id = item.getitemid (), if (id = = R.id.actionbar_overflow) {if (PopupMenu = = null) {PopupMenu = new Popup Menu (This,findviewbyid (r.id.actionbar_overflow));p opupmenu.inflate (R.menu.actionbar_pop); Popupmenu.setonmenuitemclicklistener (this);} Popupmenu.show (); return true;} else if (<pre name= "code" class= "Java" >public class Shoporderactivity extends Actionbaractivity Implementsontouchlistener, onmenuitemclicklistener{private Toast mtoast;private Context context;/** Alertdialog Input Feedback box */private EditText et_feedback;private popupmenu popupmenu; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.shop_order);//Set Home icon Area// Requestwindowfeature (Window.feature_left_icon);//setfeaturedrawableresource (Window.feature_left_icon, resId); This.context = This;mtoast = Toast.maketext (This, "", Toast.length_long); Initactionbar ();} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds iteMs to the action Bar if it is present.getmenuinflater (). Inflate (R.menu.actionbar_menu, menu); return true;} @Overridepublic boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar will//automatically handle clicks on the Home/up button so long//as you specify a parent activity in and RoidManifest.xml.int id = item.getitemid (), if (id = = R.id.actionbar_overflow) {if (PopupMenu = = null) {PopupMenu = new Pop Upmenu (This,findviewbyid (r.id.actionbar_overflow));p opupmenu.inflate (R.menu.actionbar_pop); Popupmenu.setonmenuitemclicklistener (this);} Popupmenu.show (); return true;} else if (id = = Android. R.id.home) {finish ();} else if (id = = R.id.new_order_bar) {showtip ("Show Red dots");} return super.onoptionsitemselected (item);} @Overridepublic boolean Onmenuitemclick (MenuItem arg0) {Intent intent;switch (Arg0.getitemid ()) {case R.id.actionbar_ Settings:intent = new Intent (this, settingsactivity.class); startactivity (intent); Break;case R.id.historyorder: Intent = new INtent (this, historyorderactivity.class); startactivity (intent); Break;case R.id.logout:alertdialog.builder Builder = New Alertdialog.builder (Shoporderactivity.this); Builder.settitle ("OK to exit?"). Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick ( Dialoginterface dialog, int which) {Showtip ("Log out! ");}}); Builder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {Dialog.dismiss ();}}); Builder.create (). Show (); break;} return false;} /** * Initialize ActionBar */private void Initactionbar () {ActionBar ActionBar = Getsupportactionbar (); Actionbar.settitle ("Grab order") ; actionbar.setdisplayhomeasupenabled (true); actionbar.sethomebuttonenabled (true);//Actionbar.seticon ( R.drawable.back_menu);//drawable background = (drawable) getresources ()//. Getdrawable (R.DRAWABLE.TOP_BG);// Getactionbar (). setbackgrounddrawable (background); Settitlecolor (This.getresources (). GetColor (R.color.green));// No response}/** *Show Toast * * @param str */public void Showtip (Final String str) {runonuithread (new Runnable () {@Overridepublic void run () { Mtoast.settext (str); Mtoast.show ();});}
}

id = = Android. R.id.home
The function that is implemented when the icon in the upper-left corner of the operation is returned, the configuration checklist needs to be set:

<activity
Android:name= "Com.shop.order.ShopOrderActivity"
Android:launchmode= "Singletop"
Android:theme= "@style/theme.appcompat.light.darkactionbar" >
</activity>

Actionbar_menu's Code:

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android"    xmlns:app= "Http://schemas.android.com/apk/res-auto" >        <item        android:id= "@+id/new_order_bar"        android:title= "@string/new_order"        android:icon= "@drawable/ic_launcher"        app:showasaction= "ifroom| Withtext "/>        <item        android:id=" @+id/actionbar_overflow "        android:icon=" @drawable/abc_ic_menu_ Moreoverflow_normal_holo_dark "        android:title=" @string/pop "        app:showasaction=" Ifroom|withtext "/> </menu>

Note : app:showasaction= "Ifroom|withtext" is not displayed in Actionbar if written android:showasaction= "Ifroom|withtext" When you manipulate the menu key in the lower-right corner of your phone


Actionbar_pop's Code:

<?xml version= "1.0" encoding= "Utf-8"? ><menu xmlns:android= "Http://schemas.android.com/apk/res/android" >    <item        android:id= "@+id/actionbar_settings"        android:title= "@string/settings"/>    < Item        android:id= "@+id/historyorder"        android:title= "@string/historyorder"/>    <item        android : id= "@+id/logout"        android:title= "@string/logout"/></menu>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.