ActionBarMechanics.java revision b31c3281d870e9abb673db239234d580dcc4feff
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 */
16package com.example.android.supportv7.app;
17
18import android.os.Bundle;
19import androidx.core.view.WindowCompat;
20import androidx.appcompat.app.AppCompatActivity;
21import android.view.Menu;
22import android.view.MenuItem;
23import 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 */
30public class ActionBarMechanics extends AppCompatActivity {
31    @Override
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        supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
43    }
44
45    @Override
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        return true;
65    }
66
67    @Override
68    public boolean onOptionsItemSelected(MenuItem item) {
69        Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
70        return true;
71    }
72}
73