List item width does not match parent inside dialog
Asked Answered
S

3

6

I'm trying to make a simple listView with string objects only inside dialog; the problem is the list item is not width match_parent as i set:

enter image description here

the list view is the root element:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#ff0000"
    android:id="@+id/lvLinks"
    android:layout_height="match_parent"/ >

list item:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="7dp"
    android:textSize="15sp"
    android:background="#0000ff"
    android:layout_height="match_parent" />

adapter:

ListView lv = (ListView) findViewById(R.id.lvLinks);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item_links,items);
lv.setAdapter(adapter);

I've tested it on many OS, this is not the problem

FIXED: answer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff0000"
        android:id="@+id/lvLinks"
        android:layout_height="match_parent" />
</LinearLayout>
Supplication answered 24/7, 2015 at 11:57 Comment(3)
Please provide relevant code on how the dialog is created. Is it DialogFragment?Botch
I think textview main layout is wrap_content so it is not taking full width so just check.Excusatory
I have same issue with DialogFragment and suggested answer is not workingFretwork
S
10

Why don't you put your ListView inside LinearLayout or RelativeLayout having width match_parent or fill_parent dialog.xml

Stets answered 24/7, 2015 at 12:6 Comment(4)
Yes @Supplication but you need some main Layout too, you can check it may help you. Set It's Orientation = vertical and also set width to "match_parent".Stets
this also doesnt work, sometimes items are still not stretched. Classic Google, so many bugs...Kapoor
@Kapoor Add your code. Then we are able to solve it as well.Stets
in activities it isnt a problem, but in dialogs with your custom layouts it does not work bcos there is a bug in android source code. You can try it to see :) It can not be fixed in XML (only by workarounds in code), there are plenty of similar topics in stackoverflow about it. I am sorry, I should have been more precise in my first answer that in DIALOGS it does not workKapoor
M
0

This may be not exact solution but you can try below xml for your list item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
<TextView 
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:padding="7dp"
    android:textSize="15sp"
    android:background="#0000ff"
    android:layout_height="wrap_content" /> 
</LinearLayout>
Melvinamelvyn answered 24/7, 2015 at 12:13 Comment(2)
No need to create custom adapter as it has android textview default id android:id="@android:id/text1". You can use this layout with ArrayAdapter.Melvinamelvyn
dialog even not open with that layoutSupplication
C
0

The proposed answer didn't solve anything for me. The issue actually only showed on MIUI devices, not on Google's stock nor Samsung or OnePlus.

Instead I added the following code to my custom listView constructor:

    addOnLayoutChangeListener(this);

And the listener:

@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
    int I = getChildCount();
    for(int i = 0; i < I; i++)
    {
        getChildAt(i).requestLayout();
    }
}
Cabinet answered 21/8, 2021 at 10:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.