Skip to content Skip to sidebar Skip to footer

Android: Creating Two Columns In A Linearlayout

&l

Solution 1:

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


Solution 2:

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>

Solution 3:

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


Solution 4:

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>

Solution 5:

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>

Post a Comment for "Android: Creating Two Columns In A Linearlayout"