leonheess
leonheess

Reputation: 21361

Horizontally centering text within TextView not working

This might sound very stupid but I am struggling with this so bear with me for a sec.

So I have this simple TextView that I want to have centered within the screen:

<TextView
    android:id="@+id/helloTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:includeFontPadding="false"
    android:text="Hello!"
    android:textSize="50sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.464" />

I tried it with wrap_contentand with match_constraint but I wasn't able to get it centered. When I do wrap_content the box is centered correctly but the text within is not as you can see here:

1st blueprint

and when I do match_constraint it just centers it vertically but not horizontally:

2nd blueprint

What am I missing out on?

Upvotes: 0

Views: 35

Answers (2)

Daniel Andujar
Daniel Andujar

Reputation: 1194

android:textAlignment="center"

Upvotes: 0

user8959091
user8959091

Reputation:

In your TextView's xml as you posted it add:

android:textAlignment="center"

Upvotes: 2

Related Questions