1

I'm looking for some attributes such as layoutCenteringParent in my textView and cannot find them. I want to find that specific attribute in order to center the text (I'm following an online tutorial). I tried to follow the advice here: Missing attributes in the layout design - Android Studio

and deleted my Android studio cache but it didn't help. I'm a beginner so there is a good chance I'm just missing something here. I'm attaching a screenshot:

enter image description here

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
sir-haver
  • 3,096
  • 7
  • 41
  • 85

3 Answers3

1

With ConstraintLayout you should constrain Textview from all four sides to center it.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ABC"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
1

Your best option is to use ConstraintLayout, like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/greeting"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Here is an excellent tutorial about ConstraintLayout: https://www.youtube.com/watch?v=4N4bCdyGcUc

kimwii
  • 81
  • 1
  • 7
1

To center text's container set all constraints as parent and remove any biases. To center text inside container use android:gravity = "center"

Peter Staranchuk
  • 1,343
  • 3
  • 14
  • 29