Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the menu in Android programmatically

I have two different menu layouts. I want to change the inflated menu on runtime when a button is clicked.

Initially the menu is set to @menu/navigation. I want it to change to @menu/navigation2 when button is clicked.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="th.ac.sd.sdschoolas.MainActivity">

    <FrameLayout
        android:id="@+id/content_main"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <WebView
            android:id="@+id/web_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="152dp"
            android:layout_height="158dp"
            android:layout_x="23dp"
            android:layout_y="46dp"
            app:srcCompat="@mipmap/logo"
            android:padding="20dp"/>
    </FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/navigation" />

</LinearLayout>

menu/navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav1"
        android:icon="@mipmap/homeicon"
        android:title="@string/title_home" />

    <item
        android:id="@+id/nav2"
        android:icon="@mipmap/newsicon01"
        android:title="@string/title_news" />

    <item
        android:id="@+id/nav3"
        android:icon="@drawable/ic_calendar_page_empty"
        android:title="@string/title_cal" />

    <item
        android:id="@+id/nav4"
        android:icon="@drawable/ic_chat_bubbles"
        android:title="@string/title_sms" />

    <item
        android:id="@+id/nav5"
        android:icon="@mipmap/more"
        android:title="@string/title_more" />

</menu>

menu/navigation2.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav1"
        android:icon="@mipmap/seticon"
        android:title="@string/title_setting" />

    <item
        android:id="@+id/nav2"
        android:icon="@mipmap/moreicom"
        android:title="@string/title_more2" />

</menu>
like image 750
phwt Avatar asked Mar 15 '17 08:03

phwt


People also ask

What is onOptionsItemSelected() in Android?

If your activity includes fragments, the system first calls onOptionsItemSelected() for the activity then for each fragment (in the order each fragment was added) until one returns true or all fragments have been called.

How to load menu through XML?

Give resource directory name as menu and resource type also menu. one directory will be created under res folder. To create xml resource file simply right-click on menu folder and navigate to New->Menu Resource File. Give name of file as menu_example.


1 Answers

The menu can be inflated by overriding the onCreateOptionsMenu method.

You may use an internal state to choose which menu to inflate, e.g.

private int menuToChoose = R.menu.navigation;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(menuToChoose, menu);
    return true;
}

Then, in your OnClickListener you need to change the state and call invalidateOptionsMenu(), e.g.

public void onClick(View v) {
    menuToChoose = R.menu.navigation2;

    invalidateOptionsMenu();
}
like image 73
Endzeit Avatar answered Sep 21 '22 01:09

Endzeit