ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

Center Action bar title

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.

Upvotes: 2

Views: 5532

Answers (4)

nzoth
nzoth

Reputation: 26

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
        }
    }
}

Upvotes: 0

Jéwôm'
Jéwôm'

Reputation: 3971

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()

Upvotes: 1

Jovita
Jovita

Reputation: 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>

Upvotes: -1

ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

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!!!

Upvotes: 10

Related Questions