1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.apis.app; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.view.Menu; 21 import android.view.MenuItem; 22 import android.view.Window; 23 import android.widget.Toast; 24 25 /** 26 * This demonstrates the basics of the Action Bar and how it interoperates with the 27 * standard options menu. This demo is for informative purposes only; see ActionBarUsage for 28 * an example of using the Action Bar in a more idiomatic manner. 29 */ 30 public class ActionBarMechanics extends Activity { 31 @Override onCreate(Bundle savedInstanceState)32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 35 // The Action Bar is a window feature. The feature must be requested 36 // before setting a content view. Normally this is set automatically 37 // by your Activity's theme in your manifest. The provided system 38 // theme Theme.WithActionBar enables this for you. Use it as you would 39 // use Theme.NoTitleBar. You can add an Action Bar to your own themes 40 // by adding the element <item name="android:windowActionBar">true</item> 41 // to your style definition. 42 getWindow().requestFeature(Window.FEATURE_ACTION_BAR); 43 } 44 45 @Override onCreateOptionsMenu(Menu menu)46 public boolean onCreateOptionsMenu(Menu menu) { 47 // Menu items default to never show in the action bar. On most devices this means 48 // they will show in the standard options menu panel when the menu button is pressed. 49 // On xlarge-screen devices a "More" button will appear in the far right of the 50 // Action Bar that will display remaining items in a cascading menu. 51 menu.add("Normal item"); 52 53 MenuItem actionItem = menu.add("Action Button"); 54 55 // Items that show as actions should favor the "if room" setting, which will 56 // prevent too many buttons from crowding the bar. Extra items will show in the 57 // overflow area. 58 actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); 59 60 // Items that show as actions are strongly encouraged to use an icon. 61 // These icons are shown without a text description, and therefore should 62 // be sufficiently descriptive on their own. 63 actionItem.setIcon(android.R.drawable.ic_menu_share); 64 65 return true; 66 } 67 68 @Override onOptionsItemSelected(MenuItem item)69 public boolean onOptionsItemSelected(MenuItem item) { 70 Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show(); 71 return true; 72 } 73 } 74