How to set Title in center of Toolbar in fragment

You can put text view inside toolbar like this :

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent" >
        <TextView
            android:id="@+id/title"
            android:text="Title"
            android:textSize="20dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </androidx.appcompat.widget.Toolbar>

and if you want to set the text view text to your app title use this :

TextView t = (TextView) findViewById(R.id.title) ;
t.setText(getTitle()) ;

The result :

enter image description here

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top