marson
marson

Reputation: 953

Change ActionBar icons in each tab

I want to ask, if is there some way, how to set different menu icons in ActionBar in different tabs. For example, if the user is in tab number one, in the ActionBar would appear search icon and if the user is in tab number one, there would appear refresh icon.

Is there some way, how to do that? (I am using ActionBar Fragment Navigaton Tabs)

Thanks for any advice

Upvotes: 0

Views: 1090

Answers (1)

abbas.aniefa
abbas.aniefa

Reputation: 2905

Call invalidateOptionsMenu() in onTabSelected method, which would call back onPrepareOptionsMenu . You can write menu hiding logic inside onPrepareOptionsMenu as like following.

/**
     * Called whenever we call invalidateOptionsMenu()
     */
    @Override
    public void onPrepareOptionsMenu(Menu menu) {

        super.onPrepareOptionsMenu(menu);

        try {
            if(tab 1 selected){
            menu.findItem(R.id.menu_search).setVisible(true);
            menu.findItem(R.id.menu_refresh).setVisible(false);
                }
                else if(tab 2 selected){
                    menu.findItem(R.id.menu_search).setVisible(false);
            menu.findItem(R.id.menu_refresh).setVisible(true);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Upvotes: 3

Related Questions