how to make a TextView strech across the screen, filling it up?

Viewed 84
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/pleaseChooseText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textAlignment="center"
        android:text="Please Choose your Mood"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

I want to make this text fill up screen width but not the screen height, would this work here or programatically, how do I do that?

3 Answers

use this

<TextView
android:id="@+id/pleaseChooseText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please Choose your Mood"
android:textColor="@color/white"
android:textStyle="bold"
android:layout_marginTop="60dp"
android:layout_marginBottom="15dp"
/>
    <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/pleaseChooseText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textAlignment="center"
        android:text="Please Choose your Mood"
        android:textColor="@color/black"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="5dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

The important properties are:

android:layout_height="match_parent"
android:layout_width="match_parent

I'm pretty sure what you're looking for is the AutoResizing Text Based on my previous experiences, it won't really work until you have the layout_width declared to match_parent and the layout_height declared to a hardcoded number. For example:

    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:autoSizeMaxTextSize="100sp"
        app:autoSizeMinTextSize="12sp"
        app:autoSizeStepGranularity="2sp"
        app:autoSizeTextType="uniform"
        />

There are two many missing attributes missing, but the base thing is the auto-resizing text attributes.

Related