Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android AlertDialog setView rules

The setView() method of the AlertDialog class allows one to specify a custom view for the dialog box. Are there any restrictions as to what controls can be included in this custom view ?

Also, if we set a custom view, can we still add buttons using setPositiveButton(), setNegativeButton(), etc ?

like image 426
Cygnus Avatar asked Feb 12 '13 14:02

Cygnus


People also ask

How many buttons can be set up on an AlertDialog?

AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.

Which method is used to set the list in AlertDialog in Android?

The way to make a checkbox list is to use setMultiChoiceItems . // setup the alert builder AlertDialog. Builder builder = new AlertDialog. Builder(context); builder.

What is setView Android studio?

The setView() method in AlertDialog. Builder takes any class extended from View (see it's sub classes and their sub classes). This means EditTexts, Buttons etc. But also Layouts which extend from viewGroups.

How many maximum buttons are supported in an AlertDialog?

There should not be more than three action buttons in AlertDialog, and they are positive, negative, and neutral. Positive is used to accept and continue with the action.


3 Answers

The setView() method of the AlertDialog class allows one to specify a custom view for the dialog box. Are there any restrictions as to what controls can be included in this custom view ?

The setView() method in AlertDialog.Builder takes any class extended from View (see it's sub classes and their sub classes).

This means EditTexts, Buttons etc. But also Layouts which extend from viewGroups.

Also, if we set a custom view, can we still add buttons using setPositiveButton, setNegativeButton etc ?

Yes, it only affects the body. Buttons are added below the layout.

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = 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.YourLayout, null))
        .setPositiveButton(AlertDialog.BUTTON_NEGATIVE, "Yes!",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    //
                }
         })
        .setNegativeButton(AlertDialog.BUTTON_NEGATIVE, "Cancel",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    LoginDialogFragment.this.getDialog().cancel();
                }
         });
    return builder.create();
}

UPDATE:

This answer seem to get some new activity since 2 years ago and some things have changed.

I updated the code a little bit to improve formatting and added the following tip because of the current state of best practices.

The AlertDialog defines the style and structure for your dialog, but you should use a DialogFragment as a container for your dialog. The DialogFragment class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object.

The above example is meant when you extend DialogFragment and create a AlertDialog in the onCreateDialog() callback method.

like image 122
Timmetje Avatar answered Oct 22 '22 18:10

Timmetje


In the provided documentation for AlertDialog there are no restrictions on what you could set within the view of an AlertDialog.

So the custom view will take its place under the dialog's title and above the buttons which will not be affected at all.

like image 3
ColdFire Avatar answered Oct 22 '22 19:10

ColdFire


As far as I know you can add anything you wish in setView(). The positive / negative buttons will not be affected.

like image 2
Yaroslav Mytkalyk Avatar answered Oct 22 '22 18:10

Yaroslav Mytkalyk