Set up toolbar as actionbar in fragment

84,238

Solution 1

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

Solution 2

try:

 ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

Solution 3

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.

Solution 4

Use

((ActionBarActivity) getActivity()).getSupportActionBar().setSubtitle("Your Title");

Solution 5

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)
}
Share:
84,238

Related videos on Youtube

Laurenswuyts
Author by

Laurenswuyts

Loves everything that has anything to do with mobile.

Updated on July 09, 2022

Comments

  • Laurenswuyts
    Laurenswuyts almost 2 years

    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
    Laurenswuyts over 9 years
    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?
  • Developer Paul
    Developer Paul over 9 years
    Why not just put your toolbar in your main activity and that way youll be able to use the setSupportActionBar method
  • Laurenswuyts
    Laurenswuyts over 9 years
    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
  • dominicoder
    dominicoder over 9 years
    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.
  • Machado
    Machado over 8 years
    This is returning a NullPointerException.
  • notdrone
    notdrone almost 8 years
    you need to set before you get. (that rhymes)
  • Akira Lynn
    Akira Lynn almost 6 years
    For some reason this is not initializing the toolbar in fragment
  • Acauã Pitta
    Acauã Pitta about 4 years
    what about one activity app ?
  • Acauã Pitta
    Acauã Pitta about 4 years
    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.