How to get Toolbar from fragment?
You need to cast your activity from getActivity()
to AppCompatActivity
first. Here's an example:
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle();
The reason you have to cast it is because getActivity()
returns a FragmentActivity
and you need an AppCompatActivity
In Kotlin:
(activity as AppCompatActivity).supportActionBar?.title = "My Title"
Access Toolbar (and its children) from fragment?
I'm not sure if this works, but you can try it.
final AppCompatActivity act = (AppCompatActivity) getActivity();
if (act.getSupportActionBar() != null) {
Toolbar toolbar = (Toolbar) act.getSupportActionBar().getCustomView();
}
Toolbar is essentially a ViewGroup.
Getting Toolbar in Fragment
Another way of achieving the same thing from Ellitz answer, inside the fragment access the toolbar (or any other view inside activity) directly:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
Access Toolbar (MaterialToolbar) from Fragment?
You can use:
(activity as AppCompatActivity).supportActionBar
Just a little explanation:
activity
returns a FragmentActivity
which handles an android.app.ActionBar
.
You are using an AppCompatActivity
which handles an androidx.appcompat.app.ActionBar
.
They are different.
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);
How to add Toolbar to a fragment in android?
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!
Add Toolbar inside fragment (AndroidX)
so what was the successfull answer for me:
Inside onCreate method just add setHasOptionsMenu(true);
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Inside my WallFragment.java in onCreateView I added setOnMenuItemClickListener just right after initializing the Toolbar
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View inflatedView = inflater.inflate(R.layout.fragment_wall, container, false);
TopActivityToolbar = inflatedView.findViewById(R.id.update_wall_page_toolbar);
TopActivityToolbar.setTitle("someTitle");
TopActivityToolbar.inflateMenu(R.menu.menu_app_actionbar_wall);
// and finally set click listener
TopActivityToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Your code inside Toolbar onClick...
return false;
}
});
return inflatedView;
}
Do not forget to to INFLATE the menu from res/menu/...
Related Topics
How to Disable Status Bar Click and Pull Down in Android
Using Camera in the Android Emulator
Android Studio Fails to Build New Project, Timed Out While Wating for Slave Aapt Process
Naming My Application in Android
How to Use Front and Back Camera at Same Time in Android
Validation Allow Only Number and Characters in Edit Text in Android
How to Get Number of Lines of Textview
Surfacetexture's Onframeavailable() Method Always Called Too Late
Highlight Listview Selected Row
Kotlin Error:Could Not Find Org.Jetbrains.Kotlin:Kotlin-Stdlib-Jre7:1.0.7
How to Share Image + Text Together Using Action_Send in Android
Problem Inflating Custom View for Alertdialog in Dialogfragment
"Gps" Location Provider Requires Access_Fine_Location Permission for Android 6.0
Android How to Runonuithread in Other Class
Android Alarmmanager Not Waking Phone Up