Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wrap_content for a listview's width

Is there a way to have a ListView with the with equal to the longest row? Setting wrap_content for the ListView 's width has no effect. The ListView covers the whole screen horizontally.

This is the activity layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView   android:id="@+id/listview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/country_picker_bg" />

and this is the row xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_marginBottom="@dimen/padding"
    android:padding="@dimen/padding"
    android:background="@drawable/white_round_rect" >
    <TextView android:id="@+id/title"
        style="@style/GreyTextView"
        android:layout_marginLeft="@dimen/padding"/>    
    <ImageView  
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:src="@drawable/orange_arrow_selector"/>
</LinearLayout>

Thank you, Gratzi

like image 245
Gratzi Avatar asked Jul 01 '11 11:07

Gratzi


People also ask

How do I resize a list view?

If you only want to set width and height of listview. you can set it by right clicking on listview in xml and select set width / set height and set it, for ex. 20dp, 50 dp etc..

What does Wrap_content mean?

WRAP_CONTENT means that the view wants to be just large enough to fit its own internal content, taking its own padding into account.


1 Answers

Sometimes, you know there will always be a limited number of items, maybe 5, maybe 40. In those times you still want to use a ListView and you still want to wrap content.

For those times there is this method:

/**
 * Computes the widest view in an adapter, best used when you need to wrap_content on a ListView, please be careful
 * and don't use it on an adapter that is extremely numerous in items or it will take a long time.
 *
 * @param context Some context
 * @param adapter The adapter to process
 * @return The pixel width of the widest View
 */
public static int getWidestView(Context context, Adapter adapter) {
    int maxWidth = 0;
    View view = null;
    FrameLayout fakeParent = new FrameLayout(context);
    for (int i=0, count=adapter.getCount(); i<count; i++) {
        view = adapter.getView(i, view, fakeParent);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int width = view.getMeasuredWidth();
        if (width > maxWidth) {
            maxWidth = width;
        }
    }
    return maxWidth;
}

Use it like so (notice I added some extra space to the width just in case):

listView.getLayoutParams().width = getWidestView(mContext, adapter)*1.05;
like image 121
satur9nine Avatar answered Oct 01 '22 11:10

satur9nine