proffstack
proffstack

Reputation: 45

Vertically align elements in one layout with elements in another to be at same level?

I have horizontal linear layout with two vertical linear layouts in it. Each vertical layout contains 5 TextViews and looks like this:enter image description here

As you see the 5 textviews from the first layout are at the same vertical level with the 5 textviews from the second layout. But when I change the elements of the second layout to be EditTexts it starts looking like that:

enter image description here

What can I do to align TextViews with their corresponding EditTexts to be at the exact same vertical level? I can change the margin top and bottom to all TextViews. But it's a lot of work and after that they are not at the exact same level. I'm using Xamarin.Android.

Upvotes: 0

Views: 52

Answers (2)

Hemanth S
Hemanth S

Reputation: 688

You can do easily by TableLayout it will stretch with views height and width evenly

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<TableRow>
    <TextView
        android:padding="16dp"
        android:text="Text 1"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    <EditText android:hint="Edit text 1" />
</TableRow>
<TableRow>
    <TextView
        android:padding="16dp"
        android:text="Text 2"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />

    <EditText android:hint="Edit text 2" />
</TableRow>
<TableRow>
    <TextView
        android:padding="16dp"
        android:text="Text 3"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    <EditText android:hint="Edit text 3" />
</TableRow>
<TableRow>
    <TextView
        android:padding="16dp"
        android:text="Text 4"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    <EditText android:hint="Edit text 4" />
</TableRow>
<TableRow>
    <TextView
        android:padding="16dp"
        android:text="Text 5"
        android:textAppearance="@style/TextAppearance.AppCompat.Title" />
    <EditText android:hint="Edit text 5" />
</TableRow>

Upvotes: 1

Teja
Teja

Reputation: 807

Try below code.I think it will help you. enter image description here

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:padding="10dp"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:weightSum="10"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:text="TextView1"
            android:layout_weight="2"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="match_parent"
            android:text="TextView1"
            android:layout_weight="2"
            android:layout_gravity="center"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="match_parent"
            android:text="TextView1"
            android:layout_weight="2"
            android:layout_gravity="center"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="match_parent"
            android:text="TextView1"
            android:layout_weight="2"
            android:layout_gravity="center"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_width="match_parent"
            android:text="TextView1"
            android:layout_weight="2"
            android:layout_gravity="center"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />

    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:weightSum="10"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:layout_weight="2"
            android:hint="edittext"
            android:layout_width="match_parent"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:layout_weight="2"
            android:hint="edittext"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:gravity="bottom|center"
            android:layout_weight="2"
            android:hint="edittext"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:hint="edittext"
            android:gravity="bottom|center"
            android:layout_weight="2"
            android:layout_height="wrap_content" />
        <EditText
            android:layout_width="match_parent"
            android:hint="edittext"
            android:layout_weight="2"
            android:gravity="bottom|center"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

Upvotes: 1

Related Questions