Android, How to create option Menu

后端 未结 11 1820
执笔经年
执笔经年 2020-11-27 14:11

Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake...

MenuTest.java

public class Men         


        
相关标签:
11条回答
  • 2020-11-27 14:50

    Replace return super.onCreateOptionsMenu(menu); with return true; in your onCreateOptionsMenu method This will help

    And you should also have the onCreate method in your activity

    0 讨论(0)
  • 2020-11-27 14:52

    Change your onCreateOptionsMenu method to return true. To quote the docs:

    You must return true for the menu to be displayed; if you return false it will not be shown.

    0 讨论(0)
  • 2020-11-27 14:54

    IF your Device is running Android v.4.1.2 or before,
    the menu is not displayed in the action-bar.
    But it can be accessed through the Menu-(hardware)-Button.

    0 讨论(0)
  • 2020-11-27 14:56

    you can create options menu like below:

    Menu XML 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/Menu_AboutUs"
            android:icon="@drawable/ic_about_us_over_black"
            android:title="About US"/>
        <item
            android:id="@+id/Menu_LogOutMenu"
            android:icon="@drawable/ic_arrow_forward_black"
            android:title="Logout"/>
    </menu>
    

    How you can get the menu from MENU XML(Convert menu XML to java):

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.my_options_menu,menu);
            return super.onCreateOptionsMenu(menu);
        }
    

    How to get Selected Item from Menu:

    @Override
        public boolean onOptionsItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
    
                case R.id.Menu_AboutUs:
                    //About US
                    break;
    
                case R.id.Menu_LogOutMenu:
                    //Do Logout
                    break;
            }
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
  • 2020-11-27 15:04

    Android UI programming is a little bit tricky. To enable the Options menu, in addition to the code you wrote, we also need to call setHasOptionsMenu(true) in your overriden method OnCreate(). Hope this will help you out.

    0 讨论(0)
  • 2020-11-27 15:05
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.folderview_options, menu);
        return (super.onCreateOptionsMenu(menu));
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        if (item.getItemId() == R.id.locationListRefreshLocations) {
            Cursor temp = helper.getEmployee(active_employeeId);
            String[] matches = new String[1];
            if (temp.moveToFirst()) {
                matches[0] = helper.getEmployerID(temp);
            }
            temp.close();               
            startRosterReceiveBackgroundTask(matches);
        } else if (item.getItemId()==R.id.locationListPrefs) {
            startActivity(new Intent(this, PreferencesUnlockScreen.class));
            return true;
        }           
        return super.onOptionsItemSelected(item);
    }   
    
    0 讨论(0)
提交回复
热议问题