Skip to content Skip to sidebar Skip to footer

Alert Dialog With Text Followed With A Checkbox And 2 Buttons

I have requirement to pop up alert dialog which is like a EULA screen. Which will have text describing EULA with a checkbox 'Don't show this again' and in the end 2 buttons for OK

Solution 1:

I have to agree with Mudassir, EULAs are not suppose to have "Don't show again" checkboxes, but here's how you could go about doing something like that though.

You can use a AlertDialog.Builder to build a dialog box that contains a view (which you can design in XML). Here's an example

AlertDialog.BuildereulaBuilder=newAlertDialog.Builder(this);
            LayoutInflatereulaInflater= LayoutInflater.from(this);
            VieweulaLayout= eulaInflater.inflate(R.layout.eula, null);
            eulaBuilder.setView(eulaLayout);
            CheckboxdontShowAgain= (CheckBox)eulaLayout.findViewById(R.id.dontShowAgain);
            eulaBuilder.setPositiveButton("Agree", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface d, int m) {
                    // Do something
                }
            });        
            eulaBuilder.setNegativeButton("Disagree", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface d, int m) {
                    // Do something
                }
            });
            eulaMsg = eulaBuilder.create();

What you can do is create an XML with a single object, CheckBox. Then add the view into the AlertDialog.Builder. Use AlertDialog.Builder.setMessage("EULA message here") to set your EULA message.

Solution 2:

Look at these two different method:

1) First method more simply and very fast:

CheckBoxcheckBox=newCheckBox(this);
checkBox.setText("This is your checkbox message");
LinearLayoutlinearLayout=newLinearLayout(this);
linearLayout.setLayoutParams( newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
                        LinearLayout.LayoutParams.FILL_PARENT));
linearLayout.setOrientation(1);     
linearLayout.addView(checkBox);

AlertDialog.BuilderalertDialogBuilder=newAlertDialog.Builder(this);
alertDialogBuilder.setView(linearLayout);
alertDialogBuilder.setTitle("This is the title of alert dialog");
alertDialogBuilder.setMessage("This is the message of alert dialog");
alertDialogBuilder.setPositiveButton("Ok", newDialogInterface.OnClickListener() {
    publicvoidonClick(DialogInterface arg0, int arg1) {
          // do something
     }
});
alertDialogBuilder.show();

2) Second method, with a more customized layout:

look this page, there is also the XML code to create the checkbox.

Post a Comment for "Alert Dialog With Text Followed With A Checkbox And 2 Buttons"