Question

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...)

Was it helpful?

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.

OTHER TIPS

I want to show a list with a button under it in an AlertDialog.

So What you have to do is :

Remove this from from your Button:

android:layout_below="@+id/choose_equipment_list"

Then put this in Button:

android:layout_alignParentBottom="true"

And Put this in ListView:

android:layout_above="@+id/btn_choose_equipment_close"

You are Done.

Hmm. Maybe try to use Fragments. I coded sample for you. Tell me if it is what you are looking for:)

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SampleDialogFragment dialogFragment = new SampleDialogFragment();
                dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
            }
        });
    }
}

main_activity.xml

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <Button
        android:id="@+id/open_dialog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open dialog"
        android:layout_gravity="center" />
</FrameLayout>

SampleDialogFragment.java

public class SampleDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle("Timer");
        alert.setPositiveButton("OK", null);

        View view = View.inflate(getActivity(), R.layout.dialog, null);

        String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
        ((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);

        alert.setView(view);

        return alert.create();
    }
}

dialog.xml

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

I think it could be possible to get what you need declaring in the XML your button first located in the bottom and then the listview above it with height set to fill_parent

This is should be what you want...

<?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_above="@+id/btn_choose_equipment_close"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/btn_choose_equipment_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="close" />

</RelativeLayout>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top