18

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 ?

4 Answers 4

39

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.

3
  • I am trying to follow your answer here. However, whenever I enter the line LayoutInflater inflater = getLayoutInflater(); I always get an error saying the method call requires a Bundle parameter. I tried using the savedInstanceState from the onCreateDialog parameters, but that caused new errors of its own.
    – Brian
    Oct 25, 2016 at 14:50
  • You are probably within a fragment and need getActivity().getLayoutInflater() or LayoutInflater.from(getActivity())
    – Timmetje
    Nov 3, 2016 at 13:40
  • Since SdkVersion 21 builder.setView() can take a layout res. That way you don't have to get the LayoutInflater. ex: builder.setView(R.layout.your_layout) Feb 6, 2019 at 14:49
3

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.

0
2

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

0
0

TRY, THIS EXAMPLE

  android.support.v7.app.AlertDialog.Builder adb =
    new android.support.v7.app.AlertDialog.Builder(YoreActivity.this);

  ViewGroup subView = (ViewGroup) getLayoutInflater().// inflater view
                      inflate(R.layout.yore_layout_alert, null, false);

  try {// set data of yore layout

     ((TextView) subView.findViewById(R.id.messageAlert))//get element TextView 
                                            .setText(SomeText);//set text

  } catch (NullPointerException npe) {
    npe.printStackTrace();
  }

  adb.setPositiveButton("textPOSITIVE", new DialogInterface.OnClickListener() {//one method go
    @Override
    public void onClick(DialogInterface dialog, int which) {
      //  TODO some code
    }
  });//one method end

  final android.support.v7.app.AlertDialog alertDialog =
        adb.setTitle(SomeText)// set ttile 
        .setView( subView ).create();// add view in AlertDialog.Builder, and create AlertDialog

  try { //two method go

    ((Button) subView.findViewById(R.id.customPositivButton))
    .setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        //  TODO some code
        alertDialog.dismiss();
      }
    });

  } catch (NullPointerException npe) {
    npe.printStackTrace();
  }  //two method end

  alertDialog.show(); //show in YoreActivity

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.