Search code examples
androidandroid-layoutandroid-linearlayout

Creating two columns in a linearlayout with a gap in the middle


How can I create a layout with two columns, with one textview on the left side and the other on the right side and gap in the middle?. I have already seen the answer here Android: creating two columns in a linearlayout. But I need a certain space between the two columns. Please somebody help.


Solution

  • If I understood correctly, you want to have two columns of identical width with a space in-between them. Is so:

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <!-- First column -->
    
        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="My Column 1 Text"/>
    
        </LinearLayout>
    
        <!-- Space in-between -->
    
        <Space
            android:layout_width="25dp"
            android:layout_height="match_parent" />
    
        <!-- Second column-->
    
        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="My Column 2 Text"/>
    
        </LinearLayout>
    
    </LinearLayout>