47

I want to set up my toolbar as an actionbar, but since your toolbar is a layoutelement it has to be in your layout. Now my layout is in my fragment.

I added the toolbar in my layout and I call it within my fragment:

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

It works because I can set the title and so on but now I want it to react as a actionbar because I want to have this actually. setDisplayHomeAsUpEnabled(true)

To do that I have to change the toolbar to an actionbar:

setSupportActionBar(toolbar);

That doesn't work in my fragment ...

Can anybody help me to get my toolbar to work as an actionbar in a fragment.

Laurenswuyts
  • 2,144
  • 4
  • 21
  • 39

6 Answers6

68

Now ActionBarActivity is deprecated so You need to cast your activity from getActivity() to AppCompatActivity first. Here's an example:

((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle();

The reason you have to cast it is because getActivity() returns a FragmentActivity and you need an AppCompatActivity

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
32

try:

 ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
notdrone
  • 1,793
  • 15
  • 12
18

ActionBar is an Activity property. If you want to set a toolbar from a given fragment as the ActionBar of the owning Activity, then get the Activity that owns the fragment (Fragment.getActivity()) and set its ActionBar property.

Then juse use the same setDisplayHomeAsUpEnabled method you mentioned to begin with on the ActionBar after setting your toolbar as the ActionBar to get the back / up button.

You will obviously have to manage this carefully if your app has multiple fragments within that Activity.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
dominicoder
  • 9,338
  • 1
  • 26
  • 32
  • I just want to use up navigation so I think that's why I need an actionbar made of my toolbar. How do I set that ActionBar property can you give me and example please? – Laurenswuyts Nov 20 '14 at 01:39
  • Why not just put your toolbar in your main activity and that way youll be able to use the setSupportActionBar method – Developer Paul Nov 20 '14 at 01:53
  • Because I use fragments and I set my layout in my fragments and not in my activities. So my activity cant detect the toolbar in my fragment because the fragment is loaded after the activity looks for the toolbar – Laurenswuyts Nov 20 '14 at 02:05
  • Just use the same setDisplayHomeAsUpEnabled method you mentioned to begin with on the ActionBar after setting your toolbar as the ActionBar (just tried it, works fine). Updated answer as well. – dominicoder Nov 20 '14 at 02:53
7

Use

((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("Your Title");
bummi
  • 27,123
  • 14
  • 62
  • 101
Ravi
  • 960
  • 1
  • 18
  • 41
2

If you use Kotlin, try this:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, state: Bundle?): View? { 
        (activity as AppCompatActivity).setSupportActionBar(your_toolbar)
        setHasOptionsMenu(true)

        return inflater.inflate(R.layout.your_layout, container, false)
}


override fun onCreateOptionsMenu(menu: Menu?, inflater: MenuInflater?) {
        inflater?.inflate(R.menu.your_menu, menu)
}
Miravzal
  • 2,025
  • 1
  • 13
  • 11
0

Lets say the Activity holding the fragment is MainActivity.

Do

MainActivity main = (MainActivity)getActivity();
//You can access all public variable and methods of MainActivity.
//simply call 
main.setSupportActionBar(toolbar)
main.getSupportActionBar.setTitle("title");
Amit Phaltankar
  • 3,341
  • 2
  • 20
  • 37
  • java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead. – Acauã Pitta Apr 18 '20 at 23:52