Android Add an action button

Source: Internet
Author: User

Declaring an action button in XML

All the action buttons and other items that can be used are defined in the XML file in the menu resource folder. In order to add an action button to the toolbar, you need to create a new XML file under the project/res/menu/directory.

Add a <item> element to the toolbar for each item you want to add, such as Res/menu/main_activity_actions.xml

<Menuxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:yourapp= "Http://schemas.android.com/apk/res-auto" ><!--Search, should appear as action button -    <ItemAndroid:id= "@+id/action_search"Android:icon= "@drawable/ic_action_search"Android:title= "@string/action_search"yourapp:showasaction= "Ifroom"  /> ...</Menu>

The above declares that the query action should appear as an action button when the space in the toolbar is large enough. However, the Set action button should overflow (by default, all action buttons are overflow, but it is a good practice to have a clear declaration of each action you will design).

Icons Icon Property attribute requires an image resource ID, followed by @drawable/back name must be a bitmap image, this image needs to be saved in the project directory/res/drawable/below. For example

"@drawable/ic_action_search" refers to Ic_action_search.png. (each image resource is preferably available in four different size forms placed under the Drawable-* Four folder) Similarly, the theme Title property uses a string resource, which is defined by an XML file under the Res/values/directory.

Note: When you need to create a icons icon for your program or other bitmap, be sure to provide multiple versions that allow different icons to accommodate different sizes of screens. More discussion about this will be discussed in the "Support different screens" lesson.

If you are using a support library that is compatible with the minimum to Android2.1, the Showasaction property cannot be android:namespace. Instead, this property is provided by the support library and you must define the XML namespace yourself and use this namespace as the property prefix.

Add an action to a toolbar

To add the menu item menu items to the toolbar, you can overload the Oncreateoptionsmenu () method in activity and populate the menu resource (inflate) into the given menu object. For example

@Overridepublic boolean Oncreateoptionsmenu (Menu menu) {    //inflate the menu items for use in the action Barmenuinflat Er inflater = getmenuinflater ();
Inflater.inflate (r.menu.main_activity_actions, menu); return Super.oncreateoptionsmenu (menu);}

  

Response action Button

When the user presses an action button, the system calls the activity's onoptionsitemselected () function. When overloading this method, call the Getitemid () function to determine which item in the MenuItem is pressed, the ID returned, and the Android:id attribute that responds in the <item> element.

@Overridepublic boolean onoptionsitemselected (MenuItem item) {    //Handle presses on the action bar items    switch (i Tem.getitemid ()) {case        R.id.action_search:            openSearch ();            return true;       Case r.id.action_settings:            opensettings ();            return true;        Default:            return super.onoptionsitemselected (item);}    }

  

Then call setdisplayhomeasupenabled () to make the application icon bounce up so that it can be used

@Overridepublic void OnCreate (Bundle savedinstancestate) {    super.oncreate (savedinstancestate);    Setcontentview (r.layout.activity_displaymessage);    Getsupportactionbar (). Setdisplayhomeasupenabled (true);    If your SDK version is more than 11, then use    //Getactionbar (). Setdisplayhomeasupenabled (true);}

  

Because the system knows that mainactivity is the parent activity for displaymessageactivity, when the user presses the button, the system automatically navigates to the parent activity (the user does not have to handle the button's events themselves.)

Related Article

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.