I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my
If you are using a custom toolbar, this will help you:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Feedback");
The answer is writen below in the oncreateview
method of fragments.
getActivity().setTitle("your name");
This work for me :
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.id_toolbar);
toolbar.setTitle("New Title");
For me the problem was that for some reason the label was overwritten. I had to change it back to the string resource, in
navigation.xml
inside the fragment tag;
android:label="@string/android_trivia"
This has worked for me, in Kotlin. Put this in your fragment class:
if (activity != null) {
(activity as MainActivity).supportActionBar?.title = getString(R.string.action_history)
}