jenil
jenil

Reputation: 141

Not able to align the data in listview

I have read the articles related to this but i could not modify mine as per them. I need to align all the data under headings. I have five columns with multiple rows but the data is shown as below.

enter image description here

I have used only listview in linearlayout as shown

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="55dp"
    android:orientation="horizontal"
    android:background="#F5D8BA">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="item#\nstyle#"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:textStyle="bold"
        android:typeface="serif"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="17dp"
        android:text="M Wt\nD Wt\nCS Wt"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:textStyle="bold"
        android:typeface="serif"
        />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:text="Making\nD Rate\nCS Rate"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:textStyle="bold"
        android:typeface="serif"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="7dp"
        android:text="Margin\nDis %\nDiscount"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:textStyle="bold"
        android:typeface="serif"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="GT\nFSP"
        android:layout_weight="1"
        android:textStyle="bold"
        android:textColor="@android:color/black"
        android:typeface="serif"
        />
</LinearLayout>

<LinearLayout  android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ListView android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:layout_weight="1"
        android:divider="#00000000"
        android:dividerHeight="5dp"
        android:textStyle="bold"
        android:typeface="serif"
        />
     //few items goes here
 </LinearLayout>

and list view data is like

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"
    >
<LinearLayout
    android:layout_width="75dp"
    android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12dp"
        android:textColor="#000000"
        android:text="TextView"
        android:textStyle="bold"
        android:typeface="serif"
        />

    <TextView
        android:id="@+id/textViewStyle"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="12dp"
        android:textColor="#000000"
        android:textStyle="bold"
        android:typeface="serif"
        />

</LinearLayout>
    <LinearLayout
        android:layout_width="60dp"
        android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView2"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="12dp"
        android:text="TextView"
        android:textStyle="bold"
        android:typeface="serif"
        />
    </LinearLayout>
    <LinearLayout
        android:layout_width="55dp"
        android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView3"
        android:layout_gravity="center"
        android:layout_marginLeft="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="12dp"
        android:textStyle="bold"
        android:typeface="serif"

        />
     </LinearLayout>
    <LinearLayout
        android:layout_width="65dp"
        android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView4"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textColor="#000000"
        android:textSize="12dp"
        android:textStyle="bold"
        android:typeface="serif"

        />
        </LinearLayout>
    <LinearLayout
        android:layout_width="75dp"
        android:layout_height="wrap_content" android:orientation="vertical">

    <TextView
        android:id="@+id/textView5"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:textColor="#000000"
        android:textSize="12dp"
        android:textStyle="bold"
        android:typeface="serif"

        />
    </LinearLayout>

</LinearLayout>
</ScrollView>

Upvotes: 0

Views: 63

Answers (3)

Ali Hassan
Ali Hassan

Reputation: 519

Make width of all TextView(acting as header of columns) of columns equal to the width of all LinearLayout(acting as data column) inside ScrollView as:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:background="#F5D8BA">

<TextView
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:text="item#\nstyle#"
    android:textColor="@android:color/black"
    android:layout_weight="1"
    android:textStyle="bold"
    android:typeface="serif"
    />

<TextView
    android:layout_width="60dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="17dp"
    android:text="M Wt\nD Wt\nCS Wt"
    android:textColor="@android:color/black"
    android:layout_weight="1"
    android:textStyle="bold"
    android:typeface="serif"
    />


<TextView
    android:layout_width="55dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="7dp"
    android:text="Making\nD Rate\nCS Rate"
    android:textColor="@android:color/black"
    android:layout_weight="1"
    android:textStyle="bold"
    android:typeface="serif"
    />
<TextView
    android:layout_width="65dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="7dp"
    android:text="Margin\nDis %\nDiscount"
    android:textColor="@android:color/black"
    android:layout_weight="1"
    android:textStyle="bold"
    android:typeface="serif"
    />
<TextView
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:text="GT\nFSP"
    android:layout_weight="1"
    android:textStyle="bold"
    android:textColor="@android:color/black"
    android:typeface="serif"
    />

Then keep the overall width of top LinearLayout and ScrollView same. This will do the job.

Upvotes: 1

Prasad
Prasad

Reputation: 3562

Try this

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />

            <TextView
                android:id="@+id/textViewStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="5dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView5"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="12dp"
                android:textStyle="bold"
                android:typeface="serif" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

Upvotes: 1

EnderNicky
EnderNicky

Reputation: 322

Set it to relative layout and set all of the to be alligned to the left border of the screen. Now in the design layout you will see under each and every one of the TextViews - layout:margin. Click the drop down icon and set each and every textviews margin according to where you want it to be (using dp, for example: 50dp, then the next one - 150dp and so on). A slow process, but a secure one.

Upvotes: 0

Related Questions