5

I am trying to create an AlertDialog which contains a Next and a Close button and a checkbox for "don't show it again". I use support library for DialogFragment. Following code just works fine but I want to use my own xml layout for this AlertDialog:

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt("num");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

                builder.setTitle("ASDASDAS")
                .setPositiveButton(R.string.hello_world,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((MainActivity)getActivity()).doPositiveClick();
                        }
                    }
                )
                .setNegativeButton(R.string.cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            ((MainActivity)getActivity()).doNegativeClick();
                        }
                    }
                );
                return builder.create();
    }

Is it possible to use my own xml layout to create this AlertDialog?

mctuna
  • 809
  • 3
  • 19
  • 38

3 Answers3

6

This is how to create completly custom AlertDialog in DialogFragment using your own xml layout.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
    // Add action buttons
           .setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // sign in the user ...
               }
           })
           .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
mctuna
  • 809
  • 3
  • 19
  • 38
1

Use AlertDialog.Builder#setView()

twocity
  • 646
  • 5
  • 11
  • yes exactly I have finally found it, but first we have to inflate the view as Marko pointed. I write the complete answer below, thanks – mctuna May 20 '13 at 10:34
1

You can use Dialog like this:

private void showIconsDlg(final int btnId) {
            // Use a custom style: IconsDialog
    final Dialog dlg = new Dialog(mContext, R.style.IconsDialog);
            // Use a custom layout: 
    dlg.setContentView(R.layout.your_custom_dlg);

    // Find and init Views
    GridView grid = (GridView) dlg.findViewById(R.id.icon_grid);


    grid.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
                            ...

        }
    });

    dlg.show();

}

This is Theme: IconsDialog:

<style name="IconsDialog" parent="@android:style/Theme.Dialog">
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:windowBackground">@drawable/icons_dlg_bg</item>
    <item name="android:windowNoTitle">true</item>
</style>

Set your custom alert layout in R.layout.your_custom_dlg.

herbertD
  • 10,657
  • 13
  • 50
  • 77