The default Dialogs (or rather the DialogFragments) look pretty good in Android since Honeycomb, but the default look-and-feel doesn’t go well together with all app designs. Not to mention sometimes you need a fully different layout, a custom background color, or a semitransparent background with no grey dimming at all. We’re talking about Android, where (almost) everything is possible, so there is a solution for this problem as well.

Customizing a DialogFragment is a quite easy task. Create a class extending DialogFragment, and override the onCreateDialog method, which is responsible for creating the Dialog. Instantiate a Dialog, then make the following calls on the instance:

  • requestWindowFeature(Window.FEATURE_NO_TITLE) - removes the dialog’s title
  • getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) forces the dialog to occupy the whole screen estate
  • setBackgroundDrawable() - sets the dimming drawable. This can be transparent, a solid color, a gradient or a custom drawable (even an image)
  • create the layout and set it to the dialog using setContentView()

These four steps give you an empty fullscreen layout to play with, with the option to add a semi-transparent background, revealing the underlying activity. From this point, only your imagination is the limit; a good example of what can be achieved is the following screen, taken from an upcoming app:

You can find more on using and customizing the DialogFragment at the official developer site. Here is a short sample code illustrating a DialogFragment with a yellow background and an empty layout.

public class CustomDialogFragment extends DialogFragment {

    public CustomDialogFragment() {}

    public Dialog onCreateDialog(final Bundle savedInstanceState) {

        // the content
        final RelativeLayout root = new RelativeLayout(getActivity());
        root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        // creating the fullscreen dialog
        final Dialog dialog = new Dialog(getActivity());
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.YELLOW));
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        return dialog;