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