Display the title of a fragment in a custom action bar
I want to use a custom action bar with a title.but fragment, this method only displays the app name for all fragments. I need to use the appropriate fragment name in the action bar.
getActivity().getActionBar().setDisplayShowCustomEnabled(true);
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
LayoutInflater inflator = LayoutInflater.from(getActivity());
View v = inflator.inflate(R.layout.title, null);
//if you need to customize anything else about the text, do it here.
//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
((TextView)v.findViewById(R.id.title)).setText(getActivity().getTitle());
//assign the view to the actionbar
getActivity().getActionBar().setCustomView(v);
+3
source to share
1 answer
You just need to define a method that will update the title of the action bar and call this method from any activity / fragment onResume()
In action:
@Override
public void onResume() {
// For activity
setActionbarTitle("title");
// For Fragment
((ParentActivtyNAme)getActivty).setActionbarTitle("title");
}
setActionbarTitle:
public void setActionbarTitle(String title) {
textTitle.setText(title);
}
And initialize textTitle
when you set custom view in action bar:
textTitle= ((TextView)v.findViewById(R.id.title));
Hope this helps you :)
0
source to share