Home » Android » Android: creating two columns in a linearlayout

Android: creating two columns in a linearlayout

Posted by: admin April 23, 2020 Leave a comment

Questions:
<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.

How to&Answers:

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

Answer:

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>

Answer:

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>

Answer:

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

Answer:

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>

Answer:

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

Answer:

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