Adam
Adam

Reputation: 9049

Android: creating two columns in a linearlayout

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

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Street" 
            android:layout_gravity="left"/>

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="456546546"
            android:layout_gravity="right" />

    </LinearLayout>

</LinearLayout>

I'm trying to create a layout with two columns, with one textview on the left side and the other on the right side. However, the textviews are still all on the left side.

Upvotes: 34

Views: 89304

Answers (7)

vonWippersnap
vonWippersnap

Reputation: 523

A potential issue with just having two vertical LinearLayouts for you columns is that nothing ensures rows will line up if row heights are variable. TableLayout is best for this, and also gives you a bunch of control on how columns shrink or grow to fill available space.

The link has changed since @santhosh-shettigar posted.
Guide: https://developer.android.com/guide/topics/ui/layout/grid.html
Reference: http://developer.android.com/reference/android/widget/TableLayout.html

Upvotes: 1

Thomas Decaux
Thomas Decaux

Reputation: 22711

And if you need a gap between buttons :

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

    <Button

        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.95"
        android:text="Via SMS" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.05" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.95"
        android:text="Diaporama" />
</LinearLayout>

Upvotes: 3

inazaruk
inazaruk

Reputation: 74790

You should use android:layout_weight attribute. Here is an example:

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Street" 
        android:layout_gravity="left"
        android:background="#88FF0000"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="456546546"
        android:layout_gravity="right" 
        android:background="#8800FF00"/>

</LinearLayout>

enter image description here

Upvotes: 96

Balaji
Balaji

Reputation: 2026

I hope it will helpful to you.

Try 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="horizontal">


    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="#00FF00"
        android:paddingRight="90dp"
        android:orientation="vertical" >

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b1"
            android:text="Button 1"/>

    </LinearLayout>

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FF00FF"
        android:orientation="vertical" >     

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/b2"
            android:text="Button 2"/>

    </LinearLayout>

</LinearLayout>

Upvotes: 4

MGK
MGK

Reputation: 7098

If u want to have multiple rows in each columns u can use Table Layout

Upvotes: 4

Shawn Lauzon
Shawn Lauzon

Reputation: 6282

Yeah, this one is confusing. Even though the width of LinearLayout is set to fill_parent, it still only takes the minimum width necessary. You need to set the 2nd TextView to fill_parent and then its gravity to right:

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Street"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="456546546"
        android:gravity="right" />

</LinearLayout>

Upvotes: 4

wesdfgfgd
wesdfgfgd

Reputation: 693

Try table layout when doing this. On graphical Layout Drag table layout put items in cell.

Upvotes: 1

Related Questions