You can easily get the Toolbar
from your Fragment
and then modify or change some property of that Toolbar
inside the Fragment
.
To get the Toolbar
from your Activity
you might consider using this.
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Now you need to make the changes on the Toolbar
in the onResume
function and then undo the changes each time you return from the Fragment
inside onStop
function. Otherwise the changes made in the Fragment
will be carried on to other fragments as well when switched to other Fragment
from the navigation drawer.
But in your case, I would recommend each Fragment
should have their Toolbar
so that it doesn't conflict with each other and can be modified as you need. And yes, remove the Toolbar
from your Activity
.
So add the Toolbar
in the layout of your Fragment
like this.
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimaryDark"/>
Then find it in the Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
// Modify your Toolbar here.
// ...
// For example.
// toolbar.setBackground(R.color.red);
// Create home button
AppCompatActivity activity = (AppCompatActivity) getActivity();
activity.setSupportActionBar(toolbar);
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
And Override the onOptionsItemSelected
function.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
}