Krøllebølle
Krøllebølle

Reputation: 3018

Control View size in AlertDialog

I want to create an AlertDialog as a DialogFragment with a title, an ok button, a cancel button and an ExpandableListView. The problem is that the ExpandableListView takes as much space as it likes and pushes the buttons and the title out of the dialog. What I want is the title at the top, the buttons at the bottom and the ExpandableListView to take all the rest of the space, up to fullscreen, so that the DialogFragment does not increase/decrease in size when expanding it, but rather keep it scrollable.

Here is an image describing the situation, the left one is the initialized DialogFragment, the second is after expanding one of the sections of the ExpandableListView. Nevermind the ugliness.

ExpandableListView expands too much.

I would like to achieve the following:

  1. Keep the size of the FragmentDialog fixed, preferably to the whole window (fill_parent/match_parent).
  2. Keep the buttons fixed at the bottom, the title fixed at the top and the ExpandableListView fixed (but still scrollable) in the center.

I have tried lots of various things, but here is my current take.

The custom DialogFragment

public class RecipeFilterDialogFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.recipe_filter_title);
        builder.setPositiveButton(R.string.recipe_filter_button_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // TODO: Perform filtering, fill list and return.
            }
        });
        builder.setNegativeButton(R.string.recipe_filter_button_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                // TODO: Kill dialog and return.
            }
        });

        builder.setView(getActivity().getLayoutInflater().inflate(R.layout.recipe_filter_dialog, null));
        builder.setCancelable(true);
        return builder.create();
    }

    @Override
    public void onStart() {
        super.onStart();
        AlertDialog dialog = (AlertDialog) getDialog();
        if (dialog != null)
        {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            //dialog.getWindow().setLayout(width, height);
            dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }
}

The xml file for the DialogFragment (recipe_filter_dialog.xml)

<?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" >

    <com.my.app.RecipeFilterExpandableListView
        android:id="@+id/recipe_filter_expandableListView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

The custom ExpandableListView

public class RecipeFilterExpandableListView extends ExpandableListView {

    public RecipeFilterExpandableListView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        this.setOnGroupExpandListener(new RecipeFilterDialogOnGroupExpandListener(this));

        // This adapter just fills the ExpandableListView, nevermind it.
        this.setAdapter(new RecipeFilterExpandableListAdapter(context, ((MyActivity)context).getDbFilter()));
    }
}

Upvotes: 1

Views: 468

Answers (1)

Wisnia
Wisnia

Reputation: 166

Try to set builder.setTitle(R.string.recipe_filter_title); instead of builder.setMessage(R.string.recipe_filter_title); and decide what height and width you want for your Dialog and set it in onStart() method.

Also try to do something like this:

Constructor in your dialog class:

 public RecipeFilterDialogFragment () {
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.FullscreenStyle);
    }

and style for this dialog that is used above:

<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowFullscreen">true</item>
</style>

also try to use parent style as a dialog theme f.e:

<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light.Dialog">

Upvotes: 1

Related Questions