EagerToLearn
EagerToLearn

Reputation: 675

Align TextView centerly and to the left with each other

I have 3 TextViews inside a vertical LinearLayout as below :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
    android:background="@color/green"
    tools:context=".SplashActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="FirstText"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:gravity="center"
            android:text="Second"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/lightGray"
            android:gravity="center"
            android:text="ThirdText"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>

Current output of the above code :

enter image description here

But I want the texts to be align left with each other like this :

enter image description here

How do I achieve this?

Upvotes: 0

Views: 34

Answers (2)

Ben P.
Ben P.

Reputation: 54244

You need to have a top-level parent that centers its content. Inside that, you'll have a mid-level parent that left-justifies its content. And inside that you'll have your three TextViews.

The key to making this work is to have the mid-level parent only be as wide as its contents. That way, the widest child will define the overall size of the mid-level parent, and then the top-level parent can center the group of them.

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FirstText"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ThirdText"/>

    </LinearLayout>

</FrameLayout>

enter image description here

Upvotes: 2

madu_dev
madu_dev

Reputation: 87

 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"        
    android:layout_gravity="center">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"            
        android:text="FirstText"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"      
        android:text="Second"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimary"        
        android:text="ThirdText"/>

</LinearLayout>

Upvotes: -1

Related Questions