p selva simon
p selva simon

Reputation: 51

How to hide the actionbar menu item based on tab layout?

I have a actionbar_layout and three tabs in my android application.The actionbar_layout is common for all the three tabs(Event tab,Invitation tab,Groupchat tab).The actionbar_layout have 3 icons(dropdown,event icon & settings icon). I need to visible that icons based on tab click,for example when ontap invitation tab "settings icon" only visible in actionbar layout,if click event tab only "event icon" need to be visible.Similarly when on tap Groupchat tab i need to visible "dropdown icon".

How to achieve this please help me

My menu_user_dashboard.xml code is below

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:appmunu="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="ringee.app.com.ringeeapp.UserDashBoard">

    <item
        android:id="@+id/dropdown"
        android:icon="@drawable/dropdown_icon"
        android:title="Dropdown"
        appmunu:showAsAction="always">
        <menu>
            <item
                android:id="@+id/all"
                android:title="All" />
            <item
                android:id="@+id/event"
                android:title="Event" />
            <item
                android:id="@+id/invitation"
                android:title="Invitation" />
        </menu>
    </item>

    <item
        android:id="@+id/create_occasion"
        android:icon="@drawable/ic_action_event"
        android:title="Create Occasion"
        appmunu:showAsAction="always" />

    <item
        android:id="@+id/account_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings" />

    <item
        android:id="@+id/profile"
        android:orderInCategory="100"
        android:title="@string/profile_image" />

</menu>

Upvotes: 3

Views: 1136

Answers (1)

Ashish Agrawal
Ashish Agrawal

Reputation: 1977

you can hide or show menu item in OnCreateOptionsMenu on behalf of some flags

like when user select any item then set flag true for that item and call

invalidateOptionsMenu()

after this you can change visibility

 MenuItem item = menu.findItem(R.id.menu_Done);

if (flag/condition)) {
  item.setVisible(false); 
} else { }

Upvotes: 1

Related Questions