5

I set up a toolbar in my main activity and when I go inside a fragment, I want to add a slider on it. If I had had the access to the Toolbar object, I would simply do:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);


Spinner mNavigationSpinner = new SpinnerTrigger(getSupportActionBar().getThemedContext());
toolbar.addView(mNavigationSpinner);

But if I get it using

((ActionBarActivity) getActivity()).getSupportActionBar()

I don't have any addView() method. So my question is, how can I add a view to the Toolbar in fragment if the Toolbar itself was created in an Activity.

I'm not sure if this is the best view of going about this, but I don't think I can have the Spinner in defined in the layout, because most of my fragments don't use it, they simply set a title to the toolbar. But at the same time, it would be great if I could define the toolbar once in the main activity and not redo it for every fragment.

Limon
  • 963
  • 2
  • 10
  • 23
  • when you are creating a fragment object, you can pass the toolbar object to fragment constructor. This is what i have done .. – Moinkhan Jul 13 '15 at 10:32
  • @Moinkhan how? What object should I pass to fragment? – Talha Feb 26 '16 at 14:59
  • @Talha when you are creating fragment from activity like Fragment fm = YourFragment.getInstance(yourToolbarinstance)... but i think accepted answer is good way to achive this .. – Moinkhan Feb 26 '16 at 15:31

4 Answers4

24

Another way of achieving the same thing from Ellitz answer, inside the fragment access the toolbar (or any other view inside activity) directly:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Budius
  • 39,391
  • 16
  • 102
  • 144
5

you can get it using

Toolbar refTool = ((NameOfClass)getActivity()).toolbar;

or, create an instance of your MainActivity, then, override onAttach(Activity activity) and assign your instance object of MainActivity to the activity in onAttach()

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • If there is no built in methods for this, I guess this would be the best solution. Thank you. – Limon Jul 13 '15 at 10:30
0

See toolbar main purpose is https://developer.android.com/reference/android/widget/Toolbar.html read here so there are nothing deference in toolbar and actionbar. so if you want to add view to toolbar before it set to Actionbar then toolbar.addView(your view); is fine but after apply apply to setactionbar(toolbar) or setSupportActionbar(toolbar) you can set view to actionbar.

ex. ((ActionBarActivity) getActivity()).getSupportActionBar().setView(Your view)

Thats it...

Jayesh Khasatiya
  • 2,140
  • 1
  • 14
  • 15
0

I would like to add a casting to what Budius said.

Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);

is the right way of doing it. Because

getActivity().findViewById(R.id.toolbar);

returns a view. This will give you error and you should cast it to Toolbar.

sreeragnk
  • 53
  • 2
  • 10