Android official Getting Started document [6] Add action button

Source: Internet
Author: User

Android official Getting Started document [6] Add action button Adding action Buttons
Add Action button

This lesson teaches
1.Specify the Actions in XML
2.Add the Actions to the Action Bar
3.Respond to Action Buttons
4.Add up Button for low-level activities

You should also read
? Providing up Navigation


This lesson teaches you
1. Specifying operations in XML
2. Add action to the action Bar
3. Respond to the action button
4. Add an up-up button for low-level activities

You should also read
? provide up navigation

The action Bar allows you to add buttons for the most important action items relating to the app's current context. Those that appear directly in the Action bar with an icon and/or text is known as action buttons. Actions that can ' t fit in the action bar or aren ' t important enough is hidden in the action overflow.
The action Bar allows you to add buttons to the most important action items in your application's current environment. Those directly displayed in the Action Bar have an icon and/or text that is called an action button. Action does not fit in the action bar or is not important enough in the action overflow to be hidden.

Figure 1. An action bar with a action button for Search and the action overflow, which reveals additional actions.
Figure 1. With the action key search and action overflow to reveal more actions an action bar.

Specify the Actions in XML
Operations that specify XML


--------------------------------------------------------------------------------

All action buttons and other items available in the action overflow is defined in an XML menu resource. To add actions to the Action Bar, create a new XML file in your project ' s res/menu/directory.
All the action buttons and other data items that are available in the action overflow are defined in the XML menu resource. Adds an action action to the Action Bar, creating a new XML file in the project's res/menu/directory.

Add an <item> element for each item want to include in the action Bar. For example:
Add <item> elements for each item you want to include in the action Bar. For example:

Res/menu/main_activity_actions.xml
<menu xmlns:android= "http://schemas.android.com/apk/res/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 is in the overflow-
<item android:id= "@+id/action_settings"
android:title= "@string/action_settings"
android:showasaction= "Never"/>
</menu>


Download Action Bar Icons
Download the Action Bar icon


To best match the Android iconography guidelines, your should use icons provided in the Action Bar Icon Pack.
To better match the guidelines for Android imagery, you should use the icon provided in the Action Bar icon pack.

This declares the Search action should appear as a action button when the "is" available in the action Bar, but the S Ettings action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for E Ach action.)
This declares that the search action should appear as an action button when the room is available in the Action bar, but the set action should always appear in overflow. (By default, all actions overflow, but it's a good practice to explicitly declare each action action of your design intent.) )

the icon attribute requires a resource ID for an image. The name that follows @drawable/must is the name of a bitmap image you ' ve saved in your project ' s res/drawable/directory . For example, the "@drawable/ic_action_search" refers to Ic_action_search.png. Likewise, the title attribute uses a string resource that's defined by an XML file in your project ' s res/values/directory , as discussed in Building a simple User Interface. The image resource ID required by the
Icon property. Then @drawable /Must be the name of the bitmap image that you have saved in the res/drawable/directory of your project. For example, "@drawable/ Ic_action_search" means Ic_ Action_search.png. Similarly, the Title property will be used in the XML file defined in your project's res/values/directory to discuss string resources in building a simple user interface.

note:when creating icons and other bitmap images for your app, it's important that you P Rovide multiple versions that is each optimized for a different screen density. This was discussed more in the lesson about supporting Different Screens.
Note: When your application creates icons and other bitmap images, it is important that you provide multiple versions of each optimized different screen density. This is more about supporting different screens of the course for discussion.

if your app is using the-support Library for compatibility on versions as low as Android 2.1, the Showasaction attribute is not available from the android:namespace. Instead This attribute are provided by the support Library and you must define your own XML namespace and use that Namespac e as the attribute prefix. (A custom XML namespace should be based on your app name, but it can be "any name" you want and are only accessible within th e Scope of the file in which declare it.) For example:
If your application is using a support library version low to the compatibility of the Android 2.1 system, the Showasaction property unavailable. Instead, this property is provided by the support library, and you must define your own XML namespace and use the namespace's property prefixes. (The custom XML namespace should be based on the name of your application, but it can be any name that you want to access within the scope of the file that declares it.) ) For example:

Res/menu/main_activity_actions.xml
<menu xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:yourapp= "http://schemas.android.com/apk/res-auto" >
<!--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 the Actions to the Action Bar
Add action to the action Bar


--------------------------------------------------------------------------------

To place the menu items into the action Bar, implement the Oncreateoptionsmenu () callback method in your activity to INFLA Te the menu resource into the given menu object. For example:
To apply a menu item to the Action Bar, implement your active Oncreateoptionsmenu () callback method to set the menus resource to the given menu object. For example:

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu items in the Action Bar
Menuinflater inflater = Getmenuinflater ();
Inflater.inflate (r.menu.main_activity_actions, menu);
return Super.oncreateoptionsmenu (menu);
}

Respond to Action Buttons
Respond to action button


--------------------------------------------------------------------------------

When the user presses one of the action buttons or another item in the action overflow, the system calls your activity ' s O Noptionsitemselected () callback method. In your implementation of this method, call Getitemid () on the given MenuItem to determine which item is pressed-the Retu rned ID matches the value of declared in the corresponding <item> element ' s Android:id attribute.
When the user presses one of the action buttons, or another item in the action overflow, the system calls your Active onoptionsitemselected () callback method. In implementing this method, call the given menu item Getitemid () to determine which item was pressed, return the ID, and match the value of the declaration of the corresponding <item> element to the Android:id property.

@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle presses on the action bar items
Switch (Item.getitemid ()) {
Case R.id.action_search:
OpenSearch ();
return true;
Case R.id.action_settings:
Opensettings ();
return true;
Default
return super.onoptionsitemselected (item);
}
}

Add up Button for low-level activities
To add a low-level activity activity for the UP button


--------------------------------------------------------------------------------


Figure 4. The Up button is in Gmail.
Figure 4. The Up button in Gmail.

All screens in your app ' is not ' the main entrance to your app (activities that is not the ' home ' screen) should Offe R the User A-a-navigate to the logical parent-screen in the app's hierarchy by pressing the-button in the action B Ar.
In all screens of your application, not the main portal of your application (activities not belonging to the "home page" screen) should give the user a way to navigate to the logical parent screen in the application's hierarchy by pressing the UP ARROW in the Action Bar .

When running on Android 4.1 (API level) or higher, or when using actionbaractivity from the support Library, performing Up navigation simply requires it declare the parent activity in the manifest file and enable the "Up" button for the Action Bar.
In Android4.1 (API level 16) or higher at run time, or when using actionbaractivity from the support library, performing up navigation simply requires you to declare the parent activity in the manifest file so that the up button action Bar.

For example, here's how can declare a activity ' s parent in the manifest:
For example, here is how you can declare the parent class of activity in manifest:

<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.support.PARENT_ACTIVITY"
Android:value= "Com.example.myfirstapp.MainActivity"/>
</activity>
</application>

Then enable the app icon as the "up" button by calling setdisplayhomeasupenabled ():
Then make the application icon up by calling setdisplayhomeasupenabled () to the UP button:

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_displaymessage);

Getsupportactionbar (). Setdisplayhomeasupenabled (True);
If your minsdkversion is one or higher, instead use:
Getactionbar (). Setdisplayhomeasupenabled (True);
}

Because the system now knows mainactivity are the parent activity for displaymessageactivity while the user presses the UP button, the system navigates to the parent activity as Appropriate-you does not need to handle the up button ' s event.
Because the system now knows mainactivity as the displaymessageactivity parent activity, when the user presses the UP ARROW key, the system navigates to the parent activity appropriately-if you do not need to handle the UP button event.

For more information on the navigation, see providing up navigation.
For more information about navigating up, see providing up navigation.

This article is translated from: https://developer.android.com/training/basics/actionbar/adding-buttons.html

Android Official entry document [6] Add action button

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.