Android: Putting a vertical divider/separator line between textviews in a horizontal linear layout nested in vertical linear layout?

11,694

Solution 1

Another option may be to include android:layout_margin (or separate layout_marginTop and layout_marginBottom) to create a space between the top and bottom of the drawn vertical line and the respective horizontal edges of the LinearLayout.

Other than that, I might try a RelativeLayout, and have your vertical line view adjusted to align its top and bottom to one of the adjacent TextViews.

Solution 2

You have to give a fix height either to your LinearLayout or your Vertical Seperator

Share:
11,694
Vass
Author by

Vass

This is the third phase of my StackOverflow/Stackexchange experience. Maths/Stats and the IT means to do it! (does AI fit into it or does it revolve around it? or... do they revolve around I?)

Updated on June 05, 2022

Comments

  • Vass
    Vass almost 2 years

    For an android content view, I have a vertical linearlayout with some textviews that have some lines to divide and separated the vertical elements, this works fine and the xml is below.

        <?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 xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/A" />                 
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/B" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/C" />    
        </LinearLayout>    
        <View 
             android:background="#ffffff" 
             android:layout_width = "fill_parent"
             android:layout_height="1dip"/>    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/D" />
        <View  
             android:background="#ffffff" 
             android:layout_width = "fill_parent"
             android:layout_height="1dip" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/E" />
        </LinearLayout>
    

    Now I want to add a vertical separator line between the horizontally placed text views in the nested textviews with strings A/B/C. When I try to do so by adding the hardcoded width View, the line spans the whole height from the parent linear layout.

        <?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 xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/A" />         
            <!--the vertical line separator-->
            <View  
         android:background="#ffffff" 
         android:layout_width = "1dip"
         android:layout_height="fill_parent" />         
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/B" />
            <TextView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:text="@string/C" />    
        </LinearLayout>    
        <View 
             android:background="#ffffff" 
             android:layout_width = "fill_parent"
             android:layout_height="1dip"/>    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/D" />
        <View  
             android:background="#ffffff" 
             android:layout_width = "fill_parent"
             android:layout_height="1dip" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/E" />
        </LinearLayout>
    

    For this vertical separator view I have tried to use android:layout_height="wrap_content"/> instead but the same result is presented.

    Is there a way to have a vertical separator here where the height is preserved with the introduction of a vertical line to separate textviews? Or must I choose a different layout?

  • Vass
    Vass over 11 years
    yes that is in a way a solution but it does not work well with changing the text size. I was hoping that there is a more elegant solution. I put 20dip, and yes that works. But Im worried that with hardcoded height this might cause problems later on.