2

I have this interface:

enter image description here

Using the below fragment XML. Is it possible to align the widths of the TextViews so they are all the same width?

For example, ODOMETER appears to be the longest word in a textview. How can I adjust all the other TextView's widths to match ODOMETERS? That way they all line up and the interface would be neater.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              tools:context="com.mycompany.myapp.Views.MasterDetails.VehicleFragment">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="false"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/scrollViewVehicle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <!-- YEAR -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="YEAR"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editYear"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="number"
                    android:maxLength="4"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- MAKE -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="MAKE"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <AutoCompleteTextView
                    android:id="@+id/editMake"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- MODEL -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView

                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="MODEL"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>


                <AutoCompleteTextView
                    android:id="@+id/editModel"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- BODY -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView

                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="BODY"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <AutoCompleteTextView
                    android:id="@+id/editBody"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- COLOR -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView

                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="COLOR"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <AutoCompleteTextView
                    android:id="@+id/editColor"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- TAG -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="TAG"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editTag"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>

            </LinearLayout>

            <!-- STATE -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="STATE"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <AutoCompleteTextView
                    android:id="@+id/editState"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>

            </LinearLayout>

            <!-- Odometer -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="ODOMETER"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editOdometer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- VIN -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

                <TextView

                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="VIN"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editVIN"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.7"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="17"
                    android:maxLines="1"/>

                <Button
                    android:id="@+id/btnScanVIN"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/COLOR_TXI_BLUE"
                    android:text="SCAN"
                    android:textColor="@color/COLOR_TXI_WHITE"
                    android:textStyle="bold"/>
            </LinearLayout>

            <!-- Other -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView

                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="OTHER"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editOther"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>


        </LinearLayout>


    </ScrollView>


</LinearLayout>
user-44651
  • 3,924
  • 6
  • 41
  • 87
  • Consider using a `GridLayout`. Check this out http://stackoverflow.com/questions/10016343/gridlayout-not-gridview-how-to-stretch-all-children-evenly – Pravin Sonawane Dec 02 '16 at 16:04

1 Answers1

1

Of course it is possible. You're using a horizontal layout for every couple of label-text field. Do as follow:

  1. add android:weightSum="z" to every LinearLayout
  2. add android:layout_weight="0.y" to every Label
  3. add android:layout_weight="0.x" to every Text Field

Note that 0.x + 0.y must equals to z. Obviously you need to set number values.


EDIT: This code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.mycompany.myapp.Views.MasterDetails.VehicleFragment">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="false"
        android:scrollbars="none">

        <LinearLayout
            android:id="@+id/scrollViewVehicle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <!-- YEAR -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="YEAR"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <EditText
                    android:id="@+id/editYear"
                    android:layout_width="match_parent"
                    android:layout_weight="0.8"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="number"
                    android:maxLength="4"
                    android:maxLines="1"/>
            </LinearLayout>

            <!-- MAKE -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="0.2"
                    android:paddingTop="10dp"
                    android:text="MAKE"
                    android:textColor="@color/COLOR_TXI_BLUE"
                    android:textStyle="bold"/>

                <AutoCompleteTextView
                    android:id="@+id/editMake"
                    android:layout_width="match_parent"
                    android:layout_weight="0.8"
                    android:layout_height="match_parent"
                    android:imeOptions="actionDone"
                    android:inputType="text"
                    android:maxLength="30"
                    android:maxLines="1"/>
            </LinearLayout>
        </LinearLayout>


    </ScrollView>


</LinearLayout>

Works great and I get this result enter image description here

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32