Search code examples
androidlistviewscrollviewscrollable

Button under ListView in AlertDialog is hidden


I want to show a list with a button under it in an AlertDialog. The Button is to close the Dialog itself. I extended AlertDialog and added some LayoutParams to the Window to extend the Dialog of the whole screen width:

    //Grab the window of the dialog, and change the width
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    Window window = getWindow();
    lp.copyFrom(window.getAttributes());
    //This makes the dialog take up the full width
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    window.setAttributes(lp);

ok. But if the list is long (display is full of it), I can scroll the ListView but the button is not shown under it. Here's the ContentView of the Dialog:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:id="@+id/choose_equipment_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:layout_below="@+id/choose_equipment_list"
    android:id="@+id/btn_choose_equipment_close"
    style="@style/custom_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="close" />

</RelativeLayout>

What can I do to show the button ALWAYS under the list, no matter how long it is? (I read a lot warnings to put a ListView inside a ScrollView, tried it anyway and failed...)


Solution

  • Try it:

    <?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"
        android:orientation="vertical" >
    
        <ListView
            android:id="@+id/choose_equipment_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <Button
            android:id="@+id/btn_choose_equipment_close"
            style="@style/custom_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="close" />
    
    </LinearLayout>
    

    If not work, remove the style="@style/custom_button" line. Some style configuration could change the view style and hide the button.