Search code examples
androidkotlintoolbartitletitlebar

Lager text isn't centred in toolbar


I tried to place the titles of my fragments in centre of the toolbar but one of them is misplaced... I can't figure out why...

The code I used is the next one:

     if (appCompatTextView != null) {
                val params: Toolbar.LayoutParams = Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT)
                params.gravity = Gravity.CENTER
                appCompatTextView.gravity = Gravity.CENTER
                appCompatTextView.layoutParams = params
            }

The Home tab toolbar looks like this: enter image description here

And the Android Studio tab looks like this: enter image description here

How should I place the Android Studio text in center?


Solution

  • I had similar issue when i try to center it with code. So i created an xml file with centered text. I know it's not what you are searching, but it can get the job done:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">
    
        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="32sp" />
    
    </LinearLayout>