2

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);
Community
  • 1
  • 1
N Sharma
  • 33,489
  • 95
  • 256
  • 444

2 Answers2

9

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.

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
1

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);
Foad Saeidi Nik
  • 260
  • 1
  • 5
  • 20
  • No I don't want to make it completely custom action bar. I want only the title should appear in center and rest of features remain same as like actionbar – N Sharma Jul 31 '14 at 18:12
  • What do you mean by rest of features?If you use this code as a custom action bar , rest of features will remain as like action bar! – Foad Saeidi Nik Jul 31 '14 at 18:20
  • I mean that action bar action items at appears at the right end, navigation drawer icon to open & close. Will it remain same as like Action Bar or do I need to customize everything ? I have accepted your answer please confirm it. :) – N Sharma Sep 06 '14 at 12:33
  • No , It will remain as like Action Bar :) – Foad Saeidi Nik Oct 01 '14 at 10:31