Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Creating a Custom AlertDialog in DialogFragment

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?

like image 306
mctuna Avatar asked May 19 '13 21:05

mctuna


People also ask

Is DialogFragment deprecated?

This method is deprecated. androidx. activity.

How do I create a custom dialog in Kotlin?

Getting Started. Open Android Studio and import the starter project. Create a new class CustomDialog. kt to contain our dialog.

What is the difference between dialog and DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

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();
like image 144
mctuna Avatar answered Oct 29 '22 18:10

mctuna