1

How can I center activity's action bar title in Android?

I've seen MANY questions on SO for this specific topic. And every answer comes back to "using custom view" and having your own toolbar.

I've found a solution that works without creating a custom view.

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

4 Answers4

10

Have this method in your Activity:

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 it in your onCreate():

centerTitle();

That's it!!!

ᴛʜᴇᴘᴀᴛᴇʟ
  • 4,466
  • 5
  • 39
  • 73
  • Because you have to do your own implementation of menu, style (color, theme). This way, your primary color and all other standard action bar features are still there – ᴛʜᴇᴘᴀᴛᴇʟ Feb 26 '17 at 06:21
  • I'm not sure what you mean, but using AppBarLayout and a centered TextView, you still get all the things you mentioned – OneCricketeer Feb 26 '17 at 15:21
1

In Kotlin you can do :

private fun centerTitle() {
        val textViews = ArrayList<View>()
        window.decorView.findViewsWithText(textViews, title, View.FIND_VIEWS_WITH_TEXT)
        if (textViews.size > 0) {
            var appCompatTextView: AppCompatTextView? = null
            if (textViews.size == 1)
                appCompatTextView = textViews[0] as AppCompatTextView
            else {
                for (v in textViews) {
                    if (v.parent is Toolbar) {
                        appCompatTextView = v as AppCompatTextView
                        break
                    }
                }
            }
            if (appCompatTextView != null) {
                val params = appCompatTextView.layoutParams
                params.width = ViewGroup.LayoutParams.MATCH_PARENT
                appCompatTextView.layoutParams = params
                appCompatTextView.textAlignment = View.TEXT_ALIGNMENT_CENTER
            }
        }
    }

And call centerTitle() in your onCreate()

Jéwôm'
  • 3,753
  • 5
  • 40
  • 73
0

You can use this solution. But it without ignoring another elements in your Toolbar.

fun setToolbarTextAlignment(textAlignment: Int) {
    for (i in 0..toolbarView.childCount) {
        (toolbarView.getChildAt(i) as? TextView)?.let {
            it.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            it.textAlignment = textAlignment
        }
    }
}
nzoth
  • 26
  • 4
-1
 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

          <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent" 
              android:gravity="center"
              android:text="haiii"/>
 </android.support.v7.widget.Toolbar>
Jovita
  • 1
  • 2