Android LinearLayout Line Breaks in XML

jmease picture jmease · Feb 6, 2012 · Viewed 17.1k times · Source

I have an activity designed to get customer information. First line gets name, 2nd line street address, third line is city, state and zip. I can achieve what I want using a RelativeLayout and by using android:layout_below and android:layout_toRightOf, but then i have to specifically indicate a width for my views. That width may look odd as the user reorients or switches between older and newer versions of android where everything is sized differently. I want to utilize the weight feature of a LinearLayout, but everything just comes up all on one line. How do I insert a line break to create rows of views within a LinearLayout?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/custLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"  
android:text="Name"/>
<EditText
android:id="@+id/customer"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/addressLabel"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"
android:text="Address"/>
<EditText
android:id="@+id/address"
android:layout_height="wrap_content"
android:layout_weight=".75"
android:layout_width="0dip"/>
<TextView
android:id="@+id/cityLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="City"/>
<EditText
android:id="@+id/city"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
<TextView
android:id="@+id/stateLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="State"/>
<EditText
android:id="@+id/state"
android:layout_height="wrap_content"
android:layout_weight=".20"
android:layout_width="0dip"/>
<TextView
android:id="@+id/zipLabel"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_width="0dip"
android:text="Zip"/>
<EditText
android:id="@+id/zip"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:layout_width="0dip"/>
</LinearLayout>

Answer

Dimitar Dimitrov picture Dimitar Dimitrov · Feb 6, 2012

You must use nested LinearLayout's to do this. I think something like this

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/custLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Name" />

        <EditText
            android:id="@+id/customer"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/addressLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25"
            android:text="Address" />

        <EditText
            android:id="@+id/address"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".75" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/cityLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="City" />

        <EditText
            android:id="@+id/city"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".25" />

        <TextView
            android:id="@+id/stateLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="State" />

        <EditText
            android:id="@+id/state"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />

        <TextView
            android:id="@+id/zipLabel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".1"
            android:text="Zip" />

        <EditText
            android:id="@+id/zip"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight=".20" />
    </LinearLayout>
</LinearLayout>

should work.