Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlertDialog inside onClickListener

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();
        }
    });
like image 327
Carnivoris Avatar asked Jan 11 '12 02:01

Carnivoris


People also ask

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.

Which method is used to set in AlertDialog?

setIcon() method is use to set the icon on Alert dialog box.

How many buttons can an AlertDialog display?

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

How do I use AlertDialog?

setTitle(CharSequence title)AlertDialog alertDialog = alertDialogBuilder. create(); alertDialog. show(); This will create the alert dialog and will show it on the screen.


1 Answers

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..

like image 189
R.daneel.olivaw Avatar answered Sep 28 '22 01:09

R.daneel.olivaw