Skip to content Skip to sidebar Skip to footer

Android: Listview Array Adapter Not Wrapping Content Correctly

I have a Theme.Dialog activity that presents a list of devices to a user. the activity uses a ListView with a dynamically created ArrayAdapter to display the list of devices. For s

Solution 1:

I realize that this is essentially a closed question but I believe that by setting windowIsFloating to true in a theme as seen in this answer https://stackoverflow.com/a/8876358 your problem will be fixed.

Solution 2:

I'm pretty sure it is because of the android:theme="@android:style/Theme.Dialog", those pesky dialogs with their own width and height.

Try the following: Create a Fragment that represents your ListActivity as a Listview. Placing this over your other Fragments creates a dialog-like behavior.

I'm 100% sure the problem is the Theme.Dialog. Your best shot would be changing your strategy for displaying that list. (Using Fragments, for example)

Solution 3:

i was finally able to get this to work ..i subclassed Listview and changed how it measured. It seems the listview natively will measure just the first child. So it uses the width of the first child. I changed it to search the listviews children for the widest row.

so your xml for the dialog content would look like this:

<?xml version="1.0" encoding="UTF-8"?>

<com.mypackage.myListView
    android:id="@+id/lv"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
 />

and the myListView would look like this:

publicclassMyListViewextendsListView{

publicMyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}


@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stubintmaxWidth= meathureWidthByChilds() + getPaddingLeft() + getPaddingRight();
    super.onMeasure(MeasureSpec.makeMeasureSpec(maxWidth, MeasureSpec.EXACTLY), heightMeasureSpec);     
}


 publicintmeathureWidthByChilds() {
    intmaxWidth=0;
    Viewview=null;
    for (inti=0; i < getAdapter().getCount(); i++) {
        view = getAdapter().getView(i, view, this);
        //this measures the view before its rendered with no constraints from parent
        view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        if (view.getMeasuredWidth() > maxWidth){
            maxWidth = view.getMeasuredWidth();
        }
    }
    return maxWidth;
 }
}

Post a Comment for "Android: Listview Array Adapter Not Wrapping Content Correctly"