cgr
cgr

Reputation: 4636

How to disable spinner menuitem that is in ActionBar when leaving the fragment?

I have implemented a spinner in action bar to be displayed when a fragment-1 is shown. I want it to be disappeared or disable when this fragment goes away.

Issue is that if I launch another fragment-2 on top of fragment-1 (as shown screens attached), the spinner is still shown in bar. There are similar posts (not exactly spinner as item). I either tried them and unsuccessful or I did not understand how to apply them in my case. Mark it duplicate if you see any similar post.

This is my menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menuReports"
        android:title="@string/reports_menu_title"
        app:actionLayout="@layout/layout_reports_menu"
        android:name="android:spinnerItemStyle"
        app:showAsAction="always">
    </item>
</menu>  

Fragment:

public class MyFragment extends android.support.v4.app.Fragment {
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_reports, menu);
        mSpinnerItem = menu.findItem(R.id.menuReports);

        SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(((AppCompatActivity) getActivity()).getSupportActionBar()
                .getThemedContext(), R.array.reports_types, android.R.layout.simple_spinner_dropdown_item);
    final Spinner spinner = (Spinner) MenuItemCompat.getActionView(mSpinnerItem);

    spinner.setAdapter(spinnerAdapter);

    spinner.setMinimumWidth((int) Converter.convertDpToPixels(getActivity(),
            getResources().getDimension(R.dimen.report_spinner_selected_item_width)));
    spinner.setPadding(0, 0, (int) Converter.convertDpToPixels(getActivity(), 30), 0);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Log.d(TAG, "No Item selected..");
        }
    });
}
}

Below are the two shots of bar - first one from fragment-1 and then from fragment-2. Let me know if you have any idea.

enter image description here

enter image description here

UPDATE

I tried setDisable() on spinner item btu that did not work.

Upvotes: 0

Views: 518

Answers (1)

Markus Rubey
Markus Rubey

Reputation: 5238

The best place to set up your menu is on onPrepareOptionsMenu(Menu menu). There you can hide individual menu items by calling menu.findItem(R.id.menu_item).setVisible(false);.

Small Example:

boolean isVisible = true;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.menu_item).setVisible(isVisible);
    return super.onPrepareOptionsMenu(menu);
}

private void hideMenuItem() {
    isVisible = false;
    invalidateOptionsMenu();
}

Another Example (where we call onPrepareOptionsMenu() inside Fragment):

public class MainActivity extends AppCompatActivity {

    public static final String KEY_TYPE = "key_type";

    public static final String TYPE_ACTION = "type_action";
    public static final String TYPE_NO_ACTION = "type_no_action";

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        addFragment(TYPE_NO_ACTION);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
/*
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <item
            android:id="@+id/menu_add"
            android:title="Add"
            app:showAsAction="ifRoom" />
    </menu>
*/
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_add:
                addFragment(TYPE_ACTION);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onBackPressed() {
        if (fragmentManager.getBackStackEntryCount() > 0) {
            fragmentManager.popBackStack();
        } else {
            super.onBackPressed();
        }
    }

    private void addFragment(String type) {
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE, type);

        ExampleFragment fragment = new ExampleFragment();
        fragment.setArguments(bundle);

        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(android.R.id.content, fragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    public static class ExampleFragment extends Fragment {

        private String type;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            type = getArguments().getString(KEY_TYPE, TYPE_NO_ACTION);
            setHasOptionsMenu(true);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.example_fragment, container, false);
        }

        @Override
        public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
/*
        <menu xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
            <item
                android:id="@+id/menu_action"
                android:title="Action"
                app:showAsAction="ifRoom" />
        </menu>
*/
        inflater.inflate(R.menu.menu_fragment, menu);
        }

        @Override
        public void onPrepareOptionsMenu(Menu menu) {
            MenuItem item = menu.findItem(R.id.menu_action_fragment);
            item.setVisible(type.equals(TYPE_ACTION)); // hide if type != TYPE_ACTION
            super.onPrepareOptionsMenu(menu);
        }
    }
}

Upvotes: 1

Related Questions