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
In Kotlin, I use
fun onAttach(...){
..
activity?.title = "My Title"
}
In Kotlin.
In fragment:
(activity as YourActivity).supportActionBar?.title = getString(R.string.your_title)
In activity:
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
If somebody struggles with this problem, this may be useful.
Basically you have 4 options, how to handle that:
use an interface in order to communicate with your activity, or any other convenient method, like an event bus.
you call getActivity().setTitle("Title")
, but in this case you need to attach your Toolbar
to the ActionBar
by calling the setSupportActionBar()
in your activity.
You can have a public instance of your Toolbar
and access that instance from the fragment.
Finally, if you need the instance of your Toolbar
(you may want to do something else with), you can simply get it this way:
Toolbar bar=Toolbar.class.cast(getActivity().findViewById(R.id.toolbar));
Well, the last option would solve the problem only if the Toolbar
hasn't been passed to the setSupportActionBar
method.
If it has been, then you need to call this method in your activity:
supportActionBar.setDisplayShowTitleEnabled(false)
,
which will solve the problem.
However, I would suggest to use ButterKnife which will make it a little bit cleaner, here an example:
Toolbar actionBar=findById(getActivity(),R.id.actionBar);
actionBar.setTitle("Title");
You can change the title of your toolbar on the event OnAttach, something like this
var toolbar = activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "New Title";
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
FragmentManager.BackStackEntry lastBackStackEntry=null;
int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
if(lastBackStackEntryCount >= 0 )
{
lastBackStackEntry = getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount);
}
Toast.makeText(MainActivity.this, ""+lastBackStackEntryCount, Toast.LENGTH_SHORT).show();
if(lastBackStackEntryCount == -1)
{
toolbar.setTitle("");
toolbar.setLogo(R.drawable.header_logo);
}
else
{
toolbar.setTitle(lastBackStackEntry.getName());
}
}
});
xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:background="@color/tool_bar_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Find TextView Id from toolbar
if you are using Activity
TextView mTitle = findViewById(R.id.toolbar_title);
mTitle.setText("set your title");
if you are using Fragment
TextView mTitle = view.findViewById(R.id.toolbar_title);
mTitle.setText("set your title");