ListView item won't extend width to fill_parent

Posted by slybloty on Stack Overflow See other posts from Stack Overflow or by slybloty
Published on 2012-09-24T15:36:15Z Indexed on 2012/09/24 15:37 UTC
Read the original article Hit count: 537

I have a custom ViewGroup that inflates a ListView from an xml layout. The list item layout is inflated from another xml file. All of the views are set to fill_parent. The ListView fills its parent, but the ListView items don't.

I've tried putting the ListView in a LinearLayout and assigning weight to it. Tried RelativeLayout as well. Also, I've built the ListView programmaticaly, without using the xml layout. Even changed the LayoutParams before adding the view to the ViewGroup.

I've also taken in consideration these posts as well: Width of clickable area in ListView w/ onListItemClick, In Android, how can I set a ListView item's height and width?, Android Listview width prob.

Any ideas to why the items don't extend to fill width? And how to extend them?

MyViewGroup class:

public class MyViewGroup extends ViewGroup {

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        generateMyViewGroup();

    }

    private void generateMyViewGroup()
    {
        ListView main = (ListView) View.inflate(getContext(), R.layout.layout_main, null);

        main.setAdapter(new MyAdapter(getContext()));

        this.addView(main);     
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        this.getChildAt(0).layout(l, t, r, b);
    }
}

ListView xml layout:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="3dp"
    android:background="#77000000"
    android:cacheColorHint="#00000000"
    android:divider="#00000000"
    android:dividerHeight="0dp"
    android:drawSelectorOnTop="false"
    android:scrollbars="vertical" >
</ListView>

ListView item layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_main_category"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/mainBackground"
    android:gravity="fill_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/main_category"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:paddingBottom="7dp"
        android:paddingLeft="20dp"
        android:paddingRight="5dp"
        android:paddingTop="20dp"
        android:text="test"
        android:textColor="@color/mainCategory"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

© Stack Overflow or respective owner

Related posts about android

Related posts about android-layout