Android ListView with default ArrayAdapter is oversized when displayed

477 Views Asked by At

API Level: 17

I am having trouble with changing the default size of a ListView in a file browsing app.

This is still just a prototype (see background color) but the list inside a popup window works really well! Except that it's too big and refuses to be resized to wrap the content without specifying a set pixel size for width. The height of the list is alright, but the width is all wrong. Judging from other questions, those solutions haven't been working for me.

My listview is initially defined in XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/rootSelectionPane">
    <ListView
        android:id="@+id/selectionList"
        android:background="@color/ruddy"
        android:windowIsFloating="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>
</RelativeLayout>

And the popupwindow/listview/arrayadapter are instantiated like this:

@Override public void onCreate(Bundle savedInstanceState) {

    // Other setup stuff ...

    LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

    mPopupView = layoutInflater.inflate(R.layout.file_selected_popup, null);

    mSelectListWindow = new PopupWindow(mPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    mSelectListWindow.setBackgroundDrawable(new BitmapDrawable());

    mSelectListWindow.setOutsideTouchable(true);

    ListView listView = (ListView) mPopupView.findViewById(R.id.selectionList);

    String[] fileOptions = new String[] {
            "Cut",
            "Copy",
            "Delete",
            "Rename"
    };

    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_selectable_list_item, fileOptions);

    listView.setAdapter(arrayAdapter);
    // more setup stuff after

BUT, there is still an unfortunate amount of extra space after the list of options:

screencap

I have tried setting a custom style and using it as both the layout and the listview's style, and the compiler doesn't complain, but it has absolutely no effect on the window. Is there a way to resize it intelligently?

1

There are 1 best solutions below

0
On

For people in the future who might struggle with this - I found an answer which I hadn't seen before that helped me here: Change ListView divider length based on longest Text in List

I changed my code section above to the following (still in onCreate)

LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
mPopupView = layoutInflater.inflate(R.layout.file_selected_popup, null);
mSelectListWindow = new PopupWindow(mPopupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mSelectListWindow.setBackgroundDrawable(new BitmapDrawable());
mSelectListWindow.setOutsideTouchable(true);

ListView listView = (ListView) mPopupView.findViewById(R.id.selectionList);
String[] fileOptions = new String[] {
    "Cut",
    "Copy",
    "Delete",
    "Rename"
};
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.simple_selectable_list_item, fileOptions);
listView.setAdapter(arrayAdapter);

ListAdapter listAdapter = listView.getAdapter();
int longestWidth = listAdapter.getView(0, null, listView).getMeasuredWidth();
for (int i=0; i < listAdapter.getCount(); i++) {
    View listItem = listAdapter.getView(i, null, listView);
    listItem.measure(0, 0);
    int width = listItem.getMeasuredWidth();
    if (width > longestWidth) {
        longestWidth = width;
    }
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.width = longestWidth;
listView.setLayoutParams(params);

Which produces the following results:

screen capture

2ez2pz