How can I add a menu dynamically to bottom navigation view?

24.1k views Asked by At

Android has new ui element - BottomNavigationView

enter image description here

I don't want to contain my menus in the xml files. I will receive the information about menu items and order from backend side. I want to create them dynamically and set into the BottomNavigationView in the onCreate() method. Can I do this?

3

There are 3 answers

2
ehartwell On BEST ANSWER

By default, BottomNavigationView starts with an empty menu. You can use the getMenu() method to get the Menu instance, then add menu items as in the response above. For example,

BottomNavigationView bottomNavigation = findViewById(R.id.bottom_navigation);
Menu menu = bottomNavigation.getMenu();
menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE, getString(R.string.str_menu_one))
    .setIcon(R.drawable.ic_action_one);
3
Idanatz On

Heres an example of dynamic build of menu items, in the main activity u have 2 layouts. you can get your own layout from your backend and create menu items dynamically using menu.add

src: http://www.mobiledevguide.com/2014/01/dynamically-create-menu-items-in-android.html

public class MainActivity extends Activity {

private Button mButtonOne,mButtonTwo;
private static final int MENU_ITEM_ID_ONE =1;
private static final int MENU_ITEM_ID_TWO =2;
private static final int MENU_ITEM_ID_THREE =3;
private static final int MENU_ITEM_ID_FOUR =4;
private static final int MENU_ITEM_ID_FIVE =5;
private static final int MENU_ITEM_ID_SIX =6;
private int mMenuSet = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mButtonOne=(Button) findViewById(R.id.buttonSetOne);
    mButtonTwo=(Button) findViewById(R.id.buttonSetTwo);

    mButtonOne.setOnClickListener(clickListener);
    mButtonTwo.setOnClickListener(clickListener);
}
OnClickListener clickListener=new OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.buttonSetOne) {
            mMenuSet=1;
        } else if (v.getId()==R.id.buttonSetTwo){
            mMenuSet=2;
        }
        invalidateOptionsMenu();
        /*
         * if you are using ActionBarSherlock use this.supportInvalidateOptionsMenu();
         * if you are using ActionBarCompat use invalidateOptionsMenu (Activity activity) method
         * */
    }
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    if(mMenuSet==1){
        menu.add(Menu.NONE, MENU_ITEM_ID_ONE, Menu.NONE,getString(R.string.str_menu_one)).setIcon(R.drawable.ic_action_one).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, MENU_ITEM_ID_TWO, Menu.NONE,getString(R.string.str_menu_two)).setIcon(R.drawable.ic_action_two).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, MENU_ITEM_ID_THREE, Menu.NONE,getString(R.string.str_menu_three)).setIcon(R.drawable.ic_action_three);

    }else if(mMenuSet==2){
        menu.add(Menu.NONE, MENU_ITEM_ID_FOUR, Menu.NONE,getString(R.string.str_menu_four)).setIcon(R.drawable.ic_action_four).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, MENU_ITEM_ID_FIVE, Menu.NONE,getString(R.string.str_menu_five)).setIcon(R.drawable.ic_action_five).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        menu.add(Menu.NONE, MENU_ITEM_ID_SIX, Menu.NONE,getString(R.string.str_menu_six)).setIcon(R.drawable.ic_action_six);
    }       
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_ITEM_ID_ONE:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_one), Toast.LENGTH_SHORT).show();
        break;
    case MENU_ITEM_ID_TWO:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_two), Toast.LENGTH_SHORT).show();
        break;
    case MENU_ITEM_ID_THREE:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_three), Toast.LENGTH_SHORT).show();
        break;
    case MENU_ITEM_ID_FOUR:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_four), Toast.LENGTH_SHORT).show();
        break;
    case MENU_ITEM_ID_FIVE:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_five), Toast.LENGTH_SHORT).show();
        break;
    case MENU_ITEM_ID_SIX:
        Toast.makeText(this, "Click on "+ getString(R.string.str_menu_six), Toast.LENGTH_SHORT).show();
        break;
    case R.id.action_settings:
        Toast.makeText(this, "Click on "+ getString(R.string.action_settings), Toast.LENGTH_SHORT).show();
        break;

    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}}
1
SANAT On

The easy way to use dynamic options in bottom navigation view is use different menu items like this:

switch (userType){
            case UserTypes.A:
                bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_a);
                break;
            case UserTypes.B:
                bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_b);
                break;
            case UserTypes.C:
                bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation_c);
               break;
        }