I'm trying to start an AlertDialog from an onClickListener but I'm getting the following error.
The constructor AlertDialog.Builder(new View.OnClickListener(){}) is undefined
Does anyone know how to fix this?
mRecordButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder( this )
.setTitle( "Cast Recording" )
.setMessage( "Now recording your message" )
.setPositiveButton( "Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Positive" );
}
})
.setNegativeButton( "Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Log.d( "AlertDialog", "Negative" );
}
} )
.show();
}
});
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.
setIcon() method is use to set the icon on Alert dialog box.
AlertDialog. A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout.
setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.
Change this line
new AlertDialog.Builder( this );
to
new AlertDialog.Builder( YourActivity.this );
This is because the constructor needs a Context type & OnclickListner is not a Context type
so you use the object of your Activity.
I hope it helps..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With