Bee
Bee

Reputation: 152

Inflating List view with Linear Layout, text view not getting match parent

I have a list view which is inflated with a linear layout containing six text view. I want the width of inflated text view to be match parent but its width is getting set according to value I am getting from database(Getting wrapped). I have given equal weights to textview in linear layout but width of all the textviews are different. Values of text view are coming from database using JDBC connection.

List view layout is:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff">

    <ListView
        android:id="@+id/lv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:divider="#0dcd6600"
        android:dividerHeight="2dp" >
    </ListView>

</HorizontalScrollView>

And that of row inflated in list view is:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="5dp"
    android:background="#ffffff"
    android:orientation="horizontal"
    android:weightSum="6" >

    <TextView
        android:id="@+id/txt_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_sname"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_standard"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_division"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_grno"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

    <TextView
        android:id="@+id/txt_rollno"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/border1"
        android:padding="7dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />

</LinearLayout>

So far I have tried all the below options, nothing worked.

  1. Changing view = inflater.inflate(R.layout.list_item_attendance,null);
    to
    view = inflater.inflate(R.layout.list_item_attendance, parent, false);
  2. Making List view's width to match_parent
  3. Making six linear layout for all text view and making them match parent.

Here is the image of the layout which is not proper. Text fields are not getting aligned.

And how I want it to be is like this.

Upvotes: 0

Views: 531

Answers (2)

Ashlesha Sharma
Ashlesha Sharma

Reputation: 949

This is happening due to listview in HorizontalScrollView. Weightsum and weight does not work if list view is in horizontal scroll view..

Upvotes: 1

It's not a solution but i think you are wrong here. Don't use scrollview as a parent. Please try this once. Hope it will work.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffffff" >

        <ListView
            android:id="@+id/lv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" >
        </ListView>
    </HorizontalScrollView>

</LinearLayout>

Upvotes: 0

Related Questions