Don't Be negative
Don't Be negative

Reputation: 1215

How to hide some menus in actionbar?

In my app I have nearly 8 menus and I have two types of users admin and client

for Admin I need to show all 8 manus and for user I need to show 2 menus for user

for that I have Given this

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.m_mymenu, menu);
    return true;      
}

after my code will be at on resume I am adding below one

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    SharedPreferences s = getSharedPreferences(my_New_SP, 0);
    HashMap<String, String> map= (HashMap<String, String>) s.getAll();

    int id = item.getItemId();

 if (map.get("usage").equals("Admin"))
 {
    if (id == R.id.abc) {
        Intent mypIntent = new Intent(this, Myp.class);
        startActivity(mypIntent);
        return true;
    }
    .
    .
    .
    .
    else if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }   
}

if (map.get("usage").equals("User"))
{
    if (id == R.id.web1) {
        Intent webIntent = new Intent(this, Web.class);
        startActivity(webIntent);
        return true;
    }
    else if (id == R.id.about1) {
        Intent aboutIntent = new Intent(this, About.class);
        startActivity(aboutIntent);
        return true;
    }
}   
    return super.onOptionsItemSelected(item);
}

So here In Options Menus I want to Show Only Two for user and 8 for admin..

But when I select it as user I and able to see all menus and only two of them are working.. so here I want to show only working menus remaning should hide..

can any one suggest me of this kind..

Here menus are from android menus only not dynamic menus...

Upvotes: 1

Views: 76

Answers (2)

Ranjithkumar
Ranjithkumar

Reputation: 18386

In onCreateOptionsMenu(), check for the condition and show or hide it the following way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.layout.m_mymenu, menu);


SharedPreferences s = getSharedPreferences(my_New_SP, 0);
HashMap<String, String> map= (HashMap<String, String>) s.getAll();

if (map.get("usage").equals("Admin"))
 {
 MenuItem item = menu.findItem(R.id.usermenu1);
 item.setVisible(false); 
 //your all user menu's same like above

}


else if (map.get("usage").equals("User"))
{
 MenuItem item = menu.findItem(R.id.adminmenu1);
 item.setVisible(false); 
 //your all admin menu's same like above
}

  return true;
}

Upvotes: 2

Mustanser Iqbal
Mustanser Iqbal

Reputation: 5097

private Menu menu1;

now in oncreate do something like this

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.layout.menu, menu);
        menu1 = menu.findItem(R.id.web1);
        menu1.setVisible(false);
        return true;
    }

now you can hide/show menu1 where ever you want. do same for the other options

Upvotes: 0

Related Questions