Skip to content Skip to sidebar Skip to footer

Show An Alertdialog When Button Is Clicked

I am trying to view a dialog box with radio buttons but when I clicked on the button alert dialog box never appears. Below is the code. Please let me know if there is any alternate

Solution 1:

Create this function and use showAskDialog() function in your click event

privatevoidshowAskDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("DialogTitle").setMessage("DialogMessage").setCancelable(true).setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int which) {
                  // TODO Auto-generated method stub

                }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            }).setPositiveButton("YES", new DialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });
    // before @TronicZomB advice//AlertDialog alert = builder.create();//alert.show();// after @TronicZomB advice
    builder.show();
}

Solution 2:

You need to replace return super.onCreateDialog(id); with return builder.show();. .create() will create an alert dialog from the builder but it does not display that dialog. .show() will create the dialog from the builder and show it on the screen.

I also think that the int id might be unnecessary. You could try the following code and it should work for you:

publicclassMainPageextendsActivity{
Button start;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
start = (Button) findViewById(R.id.start);
start.setBackgroundResource(R.drawable.read);
start.setOnClickListener(newView.OnClickListener() {
    @SuppressWarnings("deprecation")
    publicvoidonClick(View v) {

        showDialog(DIALOG_SINGLE_CHOICE );

    }
});

}

@OverridepublicDialogonCreateDialog() {
    AlertDialog.Builder builder = newAlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, newOnClickListener() {

        publicvoidonClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("OK", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
                publicvoidonClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            });
        return builder.show();
        }   
  }
}

Solution 3:

You're doing it wrong inside onCreateDialog().

First, you shouldn't do id = DIALOG_SINGLE_COICE but test id value if it equals to DIALOG_SINGLE_CHOICE. * If yes, then create the Dialog with AlertDialog.Builder and return builder.create(). * If no, return super.onCreateDialog(id).

But this method is deprecated, you should use DialogFragment instead.

Solution 4:

publicclassMainActivityextendsActivityimplementsOnClickListener{
private Button button;
privatestaticfinalintDIALOG_SINGLE_CHOICE=1;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    returntrue;
}

@OverridepublicvoidonClick(View v) {
    switch (v.getId()) {
    case R.id.button:
        showDialog(DIALOG_SINGLE_CHOICE);
        break;

    default:
        break;
    }
}

@Override@Deprecatedprotected Dialog onCreateDialog(int id) {
    AlertDialog.Builderbuilder=newAlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }

    }).setPositiveButton("OK", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int whichButton) {

            /* User clicked Yes so do some stuff */
        }
    }).setNegativeButton("Cancel", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int whichButton) {

            /* User clicked No so do some stuff */
        }
    }).create().show();
    returnsuper.onCreateDialog(id);
}
}

Post a Comment for "Show An Alertdialog When Button Is Clicked"