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.
I would like to achieve the following:
- Keep the size of the
FragmentDialog
fixed, preferably to the whole window (fill_parent
/match_parent
). - 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()));
}
}