Using Toolbar with Fragments
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:
((ActionBarActivity)getActivity()).setSupportActionBar(mToolbar);
UPDATE
If you're using AppCompatActivity :
((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);
I have seen a lot of answers mentioning to setSupportActionBar
for toolbar inside Fragment
but this approach may go wrong if you are having a a toolbar in Activity
and a separate Toolbar
in Fragment
.
- As you shift
setSupportActionBar
from Activity's Toolbar to Fragment's toolbar, You may face duplication ofMenuItem
even you try to override usingsetHasOptionsMenu(true)
. - Secondly If you want to update Activity's Toolbar you see your changes are not reflected because of
setSupportActionBar
inside your Fragment.
So in order to avoid this I recommend to use toolbar methods like this inside fragment to inflate menu and use
toolbar = (Toolbar) view.findViewById(R.id.toolbar_frag); toolbar.inflateMenu(R.menu.frag_menu_items); Menu menu = toolbar.getMenu();
and use Toolbar.OnMenuItemClickListener
interface to receive with menuItems click events.
Edit (Section Copied from MrEngineer13 answer)
and if you are worried about the back button you can set it like this
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back));toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //What to do on back clicked }});
Base on @Faisal Naseer answer. Here is the full example (with few notes) for using custom Toolbar
with navigation and menu in Fragment
fragment_home.xml
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent""> ... <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar_home" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:title="Home" /> </androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.kt
class HomeFragment : BaseFragment() { override fun onCreate(savedInstanceState: Bundle?) { // setHasOptionsMenu(true): don't need this anymore } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { return inflater.inflate(R.layout.fragment_home, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) toolbar_home.setNavigationIcon(R.drawable.ic_back) // need to set the icon here to have a navigation icon. You can simple create an vector image by "Vector Asset" and using here toolbar_home.setNavigationOnClickListener { // do something when click navigation } toolbar_home.inflateMenu(R.menu.menu_home) toolbar_home.setOnMenuItemClickListener { when (it.itemId) { R.id.action_add -> { // do something true } R.id.action_update -> { // do something true } else -> { super.onOptionsItemSelected(it) } } } }}
menu_home.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/action_add" android:title="@string/add_device" app:showAsAction="never" /> <item android:id="@+id/action_update_room" android:title="@string/update_room" app:showAsAction="never" /></menu>
Hope it help