Android learning route (8) Add Action button for action bar, androidaction

Source: Internet
Author: User

Android learning route (8) Add Action button for action bar, androidaction

The Action bar allows you to add an action button for the most important action items related to the current application context. The icons or text directly displayed on the action bar are calledAction buttons. Actions that are not suitable for action bar or are not so important will be hidden in action overflow.

Figure 1. An action button containing the Search function and the action overflow used to display the additional action.

Specify Actions in the XML file

All actions buttons and actions that can be accessed in action overflow are defined in an XML menu resource. To add actions to the action barres/menu/Directory to create a new XML file.

Add<item>Element. For example:


<menu xmlns:android="" >    <!-- Search, should appear as action button -->    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"          android:showAsAction="ifRoom" />    <!-- Settings, should always be in the overflow -->    <item android:id="@+id/action_settings"          android:title="@string/action_settings"          android:showAsAction="never" /></menu>
Download action bar icons

To better adapt to the Android iconography guide, you should use the icons provided in the Action Bar Icon Pack.

The XML file above declares that the Search action will be displayed as an action button when there is space in the action bar, And the Setting action will always appear in overflow. (By default, all actions come out first in overflow, but declaring different design intent for each action is actually a good thing .)

iconSpecifies an image resource ID. To@drawable/Resources that begin with must be stored in the project in the same name.res/drawable/Directory. For example,"@drawable/ic_action_search"Indicatesic_action_search.png. Similarly,titleThe property uses a string resource, which is stored in the projectres/values/The XML file under the directory is defined, as discussed in Building a Simple User Interface.

Tip:When creating icons or other bitmap images for an application, it is important to provide different optimized versions for different screen density. This topic will be discussed more in Supporting Different Screens.

If your application is using the Support Library,showAsActionAttribute cannot be usedandroid:This namespace. This property is provided by the Support Library, and you need to define your XML namespace and use it as the prefix of this property. (The custom XML namespace should be based on the Application name, but you can also get the name you want and it is only valid in the file you have declared .) For example:


<menu xmlns:android=""      xmlns:yourapp="" >    <!-- Search, should appear as action button -->    <item android:id="@+id/action_search"          android:icon="@drawable/ic_action_search"          android:title="@string/action_search"          yourapp:showAsAction="ifRoom"  />    ...</menu>
Add Actions to Action Bar

To display the menu items on the action baronCreateOptionsMenu()This callback method is used for the specified parameters.MenuThe XML file in the/menu directory corresponding to the object settings. For example:

@Overridepublic boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu items for use in the action bar    MenuInflater inflater = getMenuInflater();    inflater.inflate(, menu);    return super.onCreateOptionsMenu(menu);}
Response Action Buttons

The system callsonOptionsItemSelected()Method. In your implementation of this method, callgetItemId()To obtain the resource ID of the clicked item.<item>Theandroid:idAttribute Value.

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    // Handle presses on the action bar items    switch (item.getItemId()) {        case            openSearch();            return true;        case            openSettings();            return true;        default:            return super.onOptionsItemSelected(item);    }}
Add Up button for earlier versions of Activity

Figure 4.UpButton in Gmail.

In your application, as long as it is not your app entry activity (activities that are not the "home" screen) all the activities in must provide a logical parent interface in the application hierarchy. By clickingUpButton.

When running on Android 4.1 (API level 16) or later, or when usingActionBarActivityTo use the Up navigation, you must declare the parent activity in the manifest file and enable the Up button of action bar.

For example, the following shows how to define a parent activity in the manifest file:

<application ... >    ...    <!-- The main/home activity (it has no parent activity) -->    <activity        android:name="com.example.myfirstapp.MainActivity" ...>        ...    </activity>    <!-- A child of the main activity -->    <activity        android:name="com.example.myfirstapp.DisplayMessageActivity"        android:label="@string/title_activity_display_message"        android:parentActivityName="com.example.myfirstapp.MainActivity" >        <!-- Parent activity meta-data to support 4.0 and lower -->        <meta-data            android:name=""            android:value="com.example.myfirstapp.MainActivity" />    </activity></application>

Then, callsetDisplayHomeAsUpEnabled()Method to enable the Up button of action bar:

@Overridepublic void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_displaymessage);    getSupportActionBar().setDisplayHomeAsUpEnabled(true);    // If your minSdkVersion is 11 or higher, instead use:    // getActionBar().setDisplayHomeAsUpEnabled(true);}

Because the system knowsMainActivityYesDisplayMessageActivityWhen the user clicksUpThe system will automatically navigate to the parent activity at the time of button-youNot necessaryProcess the Click Event of the Up button.

For more information about Up Navigation, see Providing Up Navigation.

In android development, how does one implement the folding button in ActionBar?

In onPrepareOptionsMenu () of the Activity, add MenuItem through Menu. add. The default MenuItem is displayed in the fold button.
If you want MenuItem to be directly displayed on the ActionBar, you can set the MenuItem attribute: MenuItem. setShowAsAction (MenuItem. SHOW_AS_ACTION_ALWAYS );
For more questions and answers, please follow Sina Weibo @ Android mutual help platform.
How does Android make a return activity button on the Actionbar?

Click finish in the event to delete this activity. The previous activity will naturally come out,
Another silly way is to upload an action when A jumps to B, and directly jump to this action when B returns.

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: 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.