[JAVA] I opened the menu bar (option menu) on Android and saw it.

Introduction

Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well.

Target person

--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.

How to make an option menu

** (1) xml description **

    • Create an xml file for the option menu in the res / menu folder. * -> Right-click the res folder in Android Studio and use the dialog displayed by New> Android Resource File.
  1. Describe the item tag in the * menu tag. *-> One item tag is one option.
  2. Write the following 3 of the attributes of the * item tag. *

--android: id: id of this choice --android: title: Display string --app: showAsAction: Whether to show in the action bar

** (2) Submenu **

In the menu xml file, describe the combination of menu-item tags in the item tag. Submenus can be displayed by nesting.

(3)showAsAction

The app: showAsAction attribute of the item tag has the following three values.

--never: Overflow menu without displaying in action bar Store in --always: Always show in action bar (not recommended) --ifRoom: Display only when the action bar has room

(Caution) If the parent class of the activity class is not AppCompatAction but just Activity, it will be the android: showAsAction attribute.

** (4) icon **

You can specify the menu icon with the android: icon attribute of the item tag. However, the options with this attribute have the following specifications.

--The icon is not displayed in the overflow menu. --Only the icon is displayed in the action bar --If you add "| withText" to the showAsAction attribute, the menu name will also be displayed depending on the terminal size.

(Caution) You can make your own icon image, but the Android SDK also has a convenient one. To use them, specify "@android: drawable / ic_menu _...".

Sample code

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menuReset"
        android:icon="@android:drawable/ic_menu_close_clear_cancel"
        android:title="@string/menu_reset"
        app:showAsAction="always|withText"/>
    <item
        android:id="@+id/menuFonttype"
        android:title="@string/menu_fonttype"
        app:showAsAction="never"
        >
        <menu>
            <item
                android:id="@+id/menuFonttypeSerif"
                android:title="@string/menu_fonttype_serif"
                app:showAsAction="never"/>
            <item
                android:id="@+id/menuFonttypeSunserif"
                android:title="@string/meun_fonttype_sunsserif"
                app:showAsAction="never"/>
            <item
                android:id="@+id/menuFonttypeMonospace"
                android:title="@string/menu_fontttype_monospace"
                app:showAsAction="never"/>
        </menu>
    </item>
    <item
        android:id="@+id/menuFontstyle"
        android:title="@string/menu_fontstyle"
        app:showAsAction="never"
        >
        <menu>
            <item
                android:id="@+id/menuFontstyleNormal"
                android:title="@string/menu_fontstyle_normal"
                app:showAsAction="never"/>
            <item
                android:id="@+id/menuFontstyleItalic"
                android:title="@string/menu_fontstyle_italic"
                app:showAsAction="never"/>
            <item
                android:id="@+id/menuFontstyleBold"
                android:title="@string/menu_fontstyle_bold"
                app:showAsAction="never"/>
            <item
                android:id="@+id/menuFontstyleBoldItalic"
                android:title="@string/menu_fontstyle_bolditalic"
                app:showAsAction="never"/>
        </menu>
    </item>
</menu>

Display option menu

To display the option menu described in the xml file, in the activity class You need to write the ** onCreateOptionsMenu () ** method.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(Option menu xml file name, menu);
        return true;
    }

Processing at the time of selection

The process when the option menu is selected is described in the ** onCreateOptionsMenu () ** method. The argument item (Menu item type) is processed by the following pattern using this argument item for one selected menu.

  1. Get the id of the menu selected by * item.getItemId (). *
  2. Branch the process by comparing with the R value of id of the menu described in * xml. *-> Switch statement is convenient.
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
       
        int itemId = item.getItemId();
        switch (itemId) {
            case R.id.Button R value 1:
                //Process 1
                break;
            case R.id.Button R value 2:
                //Process 2
                break;
                ...
                ...
       }

TIPS

Intent back button

Describe the following in the activity class.

android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);

public boolean onOptionsItemSelected(MenuItem item) { Describe the following in the switch statement of.

case android.R.id.home:
        finish();
        db.close();
        break;

Postscript (2018-11-11)

With the above procedure, the following option menu will appear in the upper right corner. スクリーンショット 2018-11-11 15.52.32.png When you tap, ... スクリーンショット 2018-11-11 15.53.06.png If you tap "Font" again, ... スクリーンショット 2018-11-11 15.53.40.png When you tap "Font" スクリーンショット 2018-11-11 15.54.09.png

Postscript (2018-11-30)

See here for a list of icons.

that's all. It was how to put out the menu bar on Android. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.

Recommended Posts

I opened the menu bar (option menu) on Android and saw it.
I installed Docker on EC2 and started it
I stumbled on the Java version in Android Studio, so I will summarize it
I saw the list view of Android development collectively
Import the instance and use it on another screen
[Android] Display images and characters on the ViewPager tab
I want to simplify the log output on Android
I made the "Sunshine Ikezaki game" I saw on Twitter in Java.
Install the memcached plugin on MySQL and access it from Java
I want to add the disabled option to f.radio_button depending on the condition
Get the acceleration and bearing of the world coordinate system on Android
UnsupportedClassVersionError was output when I placed the .war file built with Eclipse on EC2 and executed it.
I want to download a file on the Internet using Ruby and save it locally (with caution)
[Android] Get the date on Monday
[Android] How to turn the Notification panel on and off using StatusBarManager
Add the pre-built jar library to Android and call it in the framework
[Swing] How to display an arbitrary name on the menu bar on Mac
Creating an app and deploying it for the first time on heroku
I actually expressed the older sister problem in code and calculated it
Is it possible to put the library (aar) in the Android library (aar) and use it?
The Android app crashes. Just click a button and it will fall.