1

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.

Community
  • 1
  • 1
getsilly13
  • 25
  • 7

1 Answers1

3

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(); 
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
  • 1
    Hey, this worked great! Thank you. I have not run into any problems with a few runs of it. Will continue testing. I will accept in 3 minutes. – getsilly13 Nov 20 '14 at 14:35