Reputation: 34507
Hello I am working on demo app where I need to set the title in ActionBar
in center of it. I have tried some posts of SO that did not work for me. I am trying on Android 4.3 phone.
https://stackoverflow.com/a/21770534/2455259 , https://stackoverflow.com/a/19244633/2455259
but nothing worked for me. Is there any way to set the title of ActionBar
to center in Android using appcompat ?
I am doing this like below but still title is in left side.
center_action_bar_title.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
In Activity
TextView customView = (TextView)
LayoutInflater.from(getActivity()).inflate(R.layout.center_action_bar_title,
null);
ActionBar.LayoutParams params = new
ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
customView.setText(text);
((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(customView, params);
Upvotes: 2
Views: 7540
Reputation: 4656
I know this question is very old but I was able to find a solution.
A solution that will not require you to create a custom action bar.
private void centerTitle() {
ArrayList<View> textViews = new ArrayList<>();
getWindow().getDecorView().findViewsWithText(textViews, getTitle(), View.FIND_VIEWS_WITH_TEXT);
if(textViews.size() > 0) {
AppCompatTextView appCompatTextView = null;
if(textViews.size() == 1) {
appCompatTextView = (AppCompatTextView) textViews.get(0);
} else {
for(View v : textViews) {
if(v.getParent() instanceof Toolbar) {
appCompatTextView = (AppCompatTextView) v;
break;
}
}
}
if(appCompatTextView != null) {
ViewGroup.LayoutParams params = appCompatTextView.getLayoutParams();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
appCompatTextView.setLayoutParams(params);
appCompatTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
}
}
}
Then, just call this on your onCreate()
:
centerTitle();
That's it. That simple.
Hope it helps someone.
Upvotes: 9
Reputation: 279
I think using custom action bar is the best way to customize it! You should at first build your xml file for action bar , here is an example :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_actionbar_relative_layout_main"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@color/actionbar_background"
android:enabled="false" >
<TextView
android:id="@+id/textviewactivityname"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
android:background="@android:color/transparent"
android:gravity="center_vertical"
android:text="text"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="18sp" />
</RelativeLayout>
Then you should get a refrence to this layout using LayoutInflater:
ViewGroup actionBarLayout = (ViewGroup) youractivity.getLayoutInflater().inflate(R.layout.nameofxmlfile, null);
At the end you should set this as a layout of your actionbar:
ActionBar actionBar = youractivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);
Upvotes: 1