getsilly13
getsilly13

Reputation: 25

Dynamically Adding and Removing Spinner from Toolbar in a Fragments

I have referenced this post: Change spinner style in toolbar to enable a spinner in the new toolbar. My question is if I want to add or remove this spinner based on when different fragments are displayed, how can I remove this if it is being inflated in the toolbar xml?

With the actionbar, I could add or remove menu items, can I do the same with the id of the spinner from the xml? Right now, the spinner comes up blank when it is in a fragment without me populating it with data.

Upvotes: 1

Views: 1200

Answers (1)

David Corsalini
David Corsalini

Reputation: 8208

I'm using View.setVisibility(int) to hide a Spinner in my Toolbar based on the current Fragment.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_menu, menu);
    switch (getCurrentFragment()) {
          case FRAGMENT_WITH_SPINNER:
              mSpinner.setVisibility(View.VISIBLE);
              break;
          case FRAGMENT_WITHOUT_SPINNER:
              mSpinner.setVisibility(View.GONE);
              break;
     }
        return true;
}

I do this in onCreateOptionsMenu() so it is refreshed when some component call

mActivity.invalidateOptionsMenu(); 

Upvotes: 3

Related Questions