Chit Khine Chit Khine - 5 months ago 24
Android Question

Android ListView Dividing columns with divider equally

I have a list view which contains of 3 columns.
I would like to use a divider to separate them equally in 3 parts.

For example

TEXT | TEXT | TEXT

FACE | GFRt | FOEE

However when I use a divider it is looking like this

ABC| efef| fkejfe

dffewf| feffe| efie

Any help would be greatly appreciated since I am far away from android for a long time.

My XML CODE IS AS BELOW:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/txtLabel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/divider1"
android:textAppearance="?attr/textAppearanceListItem" />

<View
android:id="@+id/divider1"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@android:color/black" />

<TextView
android:id="@+id/txtLabel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />


<View
android:id="@+id/divider2"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/divider1"
android:background="@android:color/black" />

<TextView
android:id="@+id/txtLabel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"></RelativeLayout>

</LinearLayout>

Answer

Use this layout to divide your views equally.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal">

    <TextView
        android:id="@+id/txtLabel1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/divider1"
        android:textAppearance="?attr/textAppearanceListItem"/>

    <View
        android:id="@+id/divider1"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:background="@android:color/black"/>

    <TextView
        android:id="@+id/txtLabel2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem"/>


    <View
        android:id="@+id/divider2"
        android:layout_width="1dp"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/divider1"
        android:background="@android:color/black"/>

    <TextView
        android:id="@+id/txtLabel3"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:textAppearance="?attr/textAppearanceListItem"/>


</LinearLayout>