Search code examples
androidandroid-fragmentsandroid-actionbarandroid-toolbar

How to add Toolbar to a fragment in android?


I have a fragment named as HomeFragment.java and it extends Fragment class.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.android.test.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;

public class HomeFragment extends Fragment {

    private Toolbar toolbar;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_profile,container,false);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        getActivity().setSupportActionController(toolbar);
        //setSupportActionBar(toolbar);
        return view;
    }
}

But I am getting error in the line:

getActivity().setSupportActionController(toolbar)

And the error is:

Cannot resolve method 'setSupportActionController(android.support.v7.widget.Toolbar)'

And there is a condition that I can't extend my HomeFragment to AppCombatActivity. It has to be remained as Fragment only, i.e. public class HomeFragment extends Fragment.

The condition is because of line number 5 in this piece of code

    Runnable mPendingRunnable = new Runnable() {
        @Override
        public void run() {
            // update the main content by replacing fragments
            Fragment fragment = getHomeFragment();
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.setCustomAnimations(android.R.anim.fade_in,
                    android.R.anim.fade_out);
            fragmentTransaction.replace(R.id.frame, fragment, CURRENT_TAG);
            fragmentTransaction.commitAllowingStateLoss();
        }
    };

    // If mPendingRunnable is not null, then add to the message queue
    if (mPendingRunnable != null) {
        mHandler.post(mPendingRunnable);
    }

    // show or hide the fab button
    toggleFab();

    //Closing drawer on item click
    drawer.closeDrawers();

    // refresh toolbar menu
    invalidateOptionsMenu();
}

And my getHomeFragment() is:

    private Fragment getHomeFragment() {
    switch (navItemIndex) {
        case 0:
            // home
            HomeFragment homeFragment = new HomeFragment();
            return homeFragment;
        case 1:
            // photos
            PhotosFragment photosFragment = new PhotosFragment();
            return photosFragment;
        case 2:
            // movies fragment
            MoviesFragment moviesFragment = new MoviesFragment();
            return moviesFragment;
        case 3:
            // notifications fragment
            NotificationsFragment notificationsFragment = new NotificationsFragment();
            return notificationsFragment;

        case 4:
            // settings fragment
            SettingsFragment settingsFragment = new SettingsFragment();
            return settingsFragment;
        default:
            return new HomeFragment();
    }
}

So if I change public class HomeFragment extends Fragment to public class HomeFragment extends AppCombatActivity, the return statement in the above code will give incompatible type error.

Please help me in resolving this error, I can't make toolbar or actionbar because of this error. Any help will be appreciable. Thankyou!


Solution

  • Cast your activity from getActivity() to AppCompatActivity first. I think this will solve your error:

    ((AppCompatActivity) getActivity()).getSupportActionBar(toolbar);
    

    Or in your case:

    ((AppCompatActivity) getActivity()).setSupportActionController(toolbar);
    

    getActivity() returns a FragmentActivity and you need to get AppCompatActivity returned. Hope this helps!