android: changing option menu items programmatically

前端 未结 12 1776
终归单人心
终归单人心 2020-12-02 09:33

Is it possible to change the option menu items programmatically? Can anyone provide me with an example please?

Also, I want to disable certain items, so that they do

相关标签:
12条回答
  • 2020-12-02 10:11

    Like Nikolay said do that in onPrepareOptionsMenu().

    For menu items in the action bar you have to invalidate the menu with activity.invalidateOptionsMenu();

    This is descriped in more detail here How can I refresh the ActionBar when onPrepareOptionsMenu switched menu entries?

    0 讨论(0)
  • 2020-12-02 10:15

    You can do something simple like I did. Just change the text to what is needed when the menu item is touched. I needed to turn the sound off and on, plus the ability to perform an action by touching it. Here is my code:

        @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) {
        case R.id.audioOn:
            audioOn = !audioOn;
            if (audioOn)
                item.setTitle("Audio Off");
            else
                item.setTitle("Audio On");
            return true;
    
        case R.id.touchOn:
            touchOn = !touchOn;
            if (touchOn)
                item.setTitle("Touch Off");
            else
                item.setTitle("Touch On");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    

    audioOn and touchOn are booleans checked in other parts of the code. Hope this helps.

    0 讨论(0)
  • 2020-12-02 10:22

    menu.xml

      <item 
        android:id="@+id/item1"
        android:title="your Item">
      </item>
    

    put in your java file

      public void onPrepareOptionsMenu(Menu menu) {
    
        menu.removeItem(R.id.item1);
    }
    
    0 讨论(0)
  • 2020-12-02 10:22

    Kotlin Code for accessing toolbar OptionsMenu items programmatically & change the text/icon ,..:

    1-We have our menu item in menu items file like: menu.xml, sample code for this:

     <?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/balance"
          android:title="0"
          android:orderInCategory="100"
          app:showAsAction="always" />
     </menu>
    

    2- Define a variable for accessing menu object in class :

    var menu: Menu? = null
    

    3- initial it in onCreateOptionsMenu :

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        this.menu = menu
        return true
    }
    

    4- Access the menu items inside your code or fun :

    private fun initialBalanceMenuItemOnToolbar() {
    var menuItemBalance = menu?.findItem(R.id.balance)
        menuItemBalance?.title = Balance?.toString() ?: 0.toString()
        // for change icon : menuWalletBalance?.icon
    }
    
    0 讨论(0)
  • 2020-12-02 10:26

    In case you have a BottomBar:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        if (mBottomBar.getCurrentTabId() == R.id.tab_more) {
            getMenuInflater().inflate(R.menu.more_menu, menu);
        }
    
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.preferences:
                startActivity(new Intent(this, PreferenceActivity.class));
                break;
        }
    
        return super.onOptionsItemSelected(item);
    }
    

    Then you just need to call:

    @Override
    public void onBottomBarClick(int tabId) {
        supportInvalidateOptionsMenu();
    }
    
    0 讨论(0)
  • 2020-12-02 10:26
    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.calendar, menu);
            if(show_list == true) {         
    
            if(!locale.equalsIgnoreCase("sk")) menu.findItem(R.id.action_cyclesn).setVisible(false);
    
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题