-1

i have ActionBar menu like notification and add icon but in some fragments I want hide this menu item.

Hear is my menu_dashboard.xml :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item  android:id="@+id/notification"
        android:icon="@drawable/ic_notification"
        android:title="Item 1"
        app:showAsAction="always"/>

    <item  android:id="@+id/add"
        android:icon="@drawable/ic_add"
        android:visible="false"
        android:title="Item 2"
        app:showAsAction="always"/>

</menu>

In menu_dashboard.xml see Notification is visible but Add is not Visible so i want to a show notification icon in homeFragment.class and add icon is show on another Fragment but it's not work

Hear is my code where i want show add icon :

protected void selectFragment(MenuItem item) {

        item.setChecked(true);

        switch (item.getItemId()) {
            case R.id.home:
                pushFragment(new HomeFragment());
                setTitle("Dashboard");
                break;
            case R.id.companies:
                pushFragment(new CompaniesFragment());
                setTitle("Companies");
                break;
            case R.id.contact:
                pushFragment(new ContactFragment());
                setTitle("Contact");
                break;
            case R.id.calendar:
                pushFragment(new CalenderFragment());
                setTitle("Calendar");
                break;
            case R.id.profile:
                pushFragment(new ProfileFragment());
                setTitle("My Profile");
                if (menu != null){
                    MenuItem item_down = menu.findItem(R.id.add);
                    item_down.setVisible(false);
                }
                break;
        }
    }

i refer this stackoverflow link but it's not work for me: Hide MenuItem in some Fragments

Thank in Advance

Ali
  • 3,346
  • 4
  • 21
  • 56

1 Answers1

0

I Solve this Question

if i wan to show Add icon at CompaniesFragment then:

case R.id.companies:
                pushFragment(new CompaniesFragment());
                setTitle("Companies");
                if (menu != null){
                    MenuItem item_down = menu.findItem(R.id.add);
                    item_down.setVisible(true);
                }
                break;

if i wan to hide Add icon at HomeFragment then:

case R.id.home:
                pushFragment(new HomeFragment());
                setTitle("Dashboard");
                if (menu != null){
                    MenuItem item_down = menu.findItem(R.id.add);
                    item_down.setVisible(false);
                }
                break;
Ali
  • 3,346
  • 4
  • 21
  • 56