Android: Wrap_Content Is Not Working with Listview

Android: wrap_content is not working with ListView

As Romain Guy (Google Engineer works on UI toolkit) Said in his post

By setting the width to wrap_contentyou are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.

So if you set the layout width or layout height of your ListView to wrap_content the ListView will try to measure every single view that is attached to it - which is definitely not what you want.

Keep in mind: avoid setting wrap_content for ListViews or GridViews at all times, for more details see this Google I/O video talking about the world of listview

ListView only one element shown with height = wrap_content

You can use below Utility method to set the height of your ListView based on child count.

public static void setListViewHeightBasedOnChildren(ListView myListView) {
ListAdapter adapter = myListView.getAdapter();
if (myListView != null) {
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View item= adapter.getView(i, null, myListView);
item.measure(0, 0);
totalHeight += item.getMeasuredHeight();
}

ViewGroup.LayoutParams params = myListView.getLayoutParams();
params.height = totalHeight + (myListView.getDividerHeight() * (adapter.getCount() - 1));
myListView.setLayoutParams(params);
}
}

WRAP_CONTENT not not wrapping up the listview width in android

Try this on the ListView XML:

<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#bdbdbd"
android:dividerHeight="3.0sp"
android:headerDividersEnabled="false"
android:paddingEnd="7dp"
android:paddingStart="7dp"></ListView>

I have this listview inside a FrameLayout, but you can try to have it inside a RelativeLayout.

WRAP_CONTENT of listview is not working

Never give dimensions in pixels like 80 or 100. As device changes resolution changes so it will look different on different devices. Better use following function to convert your dimension from density independent pixel(dp) to equivalent pixels according to each device's density.

So you can use DimensionUtils.dpToPx(80) instead of 80.

public final class DimensionUtils {

private static boolean isInitialised = false;
private static float pixelsPerOneDp;

// Suppress default constructor for noninstantiability.
private DimensionUtils() {
throw new AssertionError();
}

private static void initialise(View view) {
pixelsPerOneDp = view.getResources().getDisplayMetrics().densityDpi / 160f;
isInitialised = true;
}

public static float pxToDp(View view, float px) {
if (!isInitialised) {
initialise(view);
}

return px / pixelsPerOneDp;
}

public static float dpToPx(View view, float dp) {
if (!isInitialised) {
initialise(view);
}

return dp * pixelsPerOneDp;
}
}

Why do ListView items not grow to wrap their content?

I managed to fix this, but I don't understand why.

As I mentioned, I had set the layout_height of the list item layout to wrap_content (since fill_parent is meaningless here, considering that a ListView is indefinitely tall).

However, I had set the layout_height of all views inside that layout to fill_parent. The problem disappeared when setting them to wrap_content instead.

This raises two other questions:

1) What are the semantics of a view asking to fill_parent, when the parent wraps_content? Which size request takes precedence?

2) How would I ever make a view fill a list item if fill_parent apparently doesn't work?

Thanks for your input guys.

ListView ignoring wrap_content

It appears you're trying to add a header to your listview. Listview has built-in support for headers (and footers). The method for setting a header is listview.addHeader(View view). You can customize the header as needed.

Android LinearLayout weight and wrap_content of ListView

As it is said in here and in here, standart android ListView can not have wrap_content as a width since the width of child elements may be different.

As a workaround, one should use RecycleView instead of ListView.



Related Topics



Leave a reply



Submit