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.androidx.app;
17 
18 import android.os.Bundle;
19 import android.view.Menu;
20 import android.view.MenuItem;
21 import android.widget.Toast;
22 
23 import androidx.appcompat.app.AppCompatActivity;
24 import androidx.core.view.WindowCompat;
25 
26 /**
27  * This demonstrates the basics of the Action Bar and how it interoperates with the
28  * standard options menu. This demo is for informative purposes only; see ActionBarUsage for
29  * an example of using the Action Bar in a more idiomatic manner.
30  */
31 public class ActionBarMechanics extends AppCompatActivity {
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         // The Action Bar is a window feature. The feature must be requested
37         // before setting a content view. Normally this is set automatically
38         // by your Activity's theme in your manifest. The provided system
39         // theme Theme.WithActionBar enables this for you. Use it as you would
40         // use Theme.NoTitleBar. You can add an Action Bar to your own themes
41         // by adding the element <item name="android:windowActionBar">true</item>
42         // to your style definition.
43         supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
44     }
45 
46     @Override
onCreateOptionsMenu(Menu menu)47     public boolean onCreateOptionsMenu(Menu menu) {
48         // Menu items default to never show in the action bar. On most devices this means
49         // they will show in the standard options menu panel when the menu button is pressed.
50         // On xlarge-screen devices a "More" button will appear in the far right of the
51         // Action Bar that will display remaining items in a cascading menu.
52         menu.add("Normal item");
53 
54         MenuItem actionItem = menu.add("Action Button");
55 
56         // Items that show as actions should favor the "if room" setting, which will
57         // prevent too many buttons from crowding the bar. Extra items will show in the
58         // overflow area.
59         actionItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
60 
61         // Items that show as actions are strongly encouraged to use an icon.
62         // These icons are shown without a text description, and therefore should
63         // be sufficiently descriptive on their own.
64         actionItem.setIcon(android.R.drawable.ic_menu_share);
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