android: listview item width fill_parent doesnt work
Asked Answered
A

3

12

I have search the internet and so on but couldnt find a solution for my problem. I'm creating a listview and setting an adapter:

myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, theStringList));

theStringList is a simple LinkedList<String> with items.

and the android.R.layout.simple_list_item_1 is:

<TextView android:id="@android:id/text1" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:textAppearance="?android:attr/textAppearanceLarge" 
  android:gravity="center_vertical" 
  android:paddingLeft="6dip" 
  android:minHeight="?android:attr/listPreferredItemHeight"/>;

problem: the listView items only fit the text but NOT the whole screen?!?!?

Alicealicea answered 29/12, 2010 at 9:39 Comment(0)
E
30

How are you creating your ListView? Is it defined in XML? Are you using a ListActivity? Or are you creating it programmatically? You need to make sure that the ListView itself has a width of fill_parent.

Eager answered 29/12, 2010 at 14:8 Comment(0)
F
3

Who is the parent view of your TextView? Check the settings for that one too, maybe it's a fixed width smaller than the screen or seted to "wrap_content" .

Forzando answered 29/12, 2010 at 9:54 Comment(0)
F
1

If your ListView is inside ConstraintLayout, as it is in my case, try setting your ListView's layout_width=0dp (like match_parent in other layouts) to match the constraint.

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/list_stocks"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="3dp"
        android:padding="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView4" />

</androidx.constraintlayout.widget.ConstraintLayout>
Fluoric answered 25/11, 2021 at 9:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.