Al Hennessey Al Hennessey - 1 year ago 221
Java Question

Using Toolbar with Fragments

i am trying to create a viewpager that swipes through 3 different fragments each with a different toolbar. I have implemented the new toolbar in an activity before and got it to work however i am trying to get it to work with fragments

Here is the fragment code

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout resource that'll be returned
View rootView = inflater.inflate(R.layout.fragment_home, container, false);

mToolbar = (Toolbar) rootView.findViewById(;
if (mToolbar != null) {

return rootView;

I am extending my fragment with
, however i am getting the error

Cannot resolve method setSupportActionBar

I am not sure how to resolve this, if i remove the
code will it stop working with certain devices?

Answer Source

Fragments don't have such method setSupportActionBar(). ActionBar is a property of Activity, so to set your toolbar as the actionBar, your activity should extend from ActionBarActivity and then you can call in your Fragment:



If you're using AppCompatActivity :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download