Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make menuItem visible and invisible

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 10.0k
    Comment on it

    If you are looking how to create menuItem and make them visible or invisible on certain condition, here is way to create menuItems:-

    Step 1:- Create items on menu.xml:-

    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:com.demo="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity">
      <item android:id="@+id/menu_search"
            android:icon="@drawable/search"
            android:title="Setting"
            com.demo:showAsAction="always"/> 
       <item android:id="@+id/menu_setting"
            android:icon="@drawable/setting"
            android:title="Setting"
            com.selectorsdemo:showAsAction="always"/>
    </menu>
    

    Step 2:- Code snippet in your main activity:-

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
      {
         Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
          tb.inflateMenu(R.menu.menu_main);
         MenuItem itemSearch = menu.findItem(R.id.menu_search);
         MenuItem itemSetting = menu.findItem(R.id.menu_setting);
    
         if (count % 2 == 0) 
           {
             itemSetting.setVisible(true);
            }
         else
            {
              itemSetting.setVisible(false);
            }
    
         tb.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener()
            {
            @Override
            public boolean onMenuItemClick(MenuItem item) 
               {
                 return onOptionsItemSelected(item);
               }
             });
          return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_search:
                Toast.makeText(this, "Search selected", Toast.LENGTH_SHORT)
                        .show();
                break;
            case R.id.menu_setting:
                Toast.makeText(this, "Action Settings selected", Toast.LENGTH_SHORT)
                        .show();
                break;
    
            default:
                break;
        }
    
        return true;
    }
    

    step 3:-apply invalidateOptionsMenu() function after the code where your menu items are changing:-

    In this example , the visibility of MenuItem is changing according to value of "count", so on ButtonClick when value of count gets changed then invalidateOptionsMenu() is called;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Toolbar tb = (Toolbar) findViewById(R.id.toolbar);
            ImageView homeIcon = (ImageView) tb.findViewById(R.id.toolbar_home);
            homeIcon.setOnClickListener(new View.OnClickListener()
             {
                @Override
                public void onClick(View view) 
                 {
                   count++;
                    invalidateOptionsMenu();
                }
            });
            setSupportActionBar(tb);
         }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: