How to set Title in center of Toolbar in fragment

Viewed 332

How can i set Title in center of toolbar in fragment.

I have tried various way but They didn't work. I want to set Tittle in center with bold style.

enter image description here

enter image description here

i want to set these tittles in center with bold style

1 Answers

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

Related