Search code examples
androidbuttoncustomdialog

Button in Custom Dialog Android


This is my code

    protected void markerTouched(Marker marker) {


                final Dialog dialog = new Dialog(context);


                dialog.setContentView(R.layout.custom_dialog);


                dialog.setCanceledOnTouchOutside(true);

                dialog.show();      

                 Button declineButton = (Button) dialog.findViewById(R.id.b_close);
                    // if decline button is clicked, close the custom dialog
                    declineButton.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            // Close dialog
                            dialog.dismiss();
                        }
                    });
     }

i have found an error in this line:

 declineButton.setOnClickListener(new OnClickListener() 

the error on setOnClickListner:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){})

and other in OnClickListener():

The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)

someone can explain what i've to do?


Solution

  • Try this:

    dialog = new Dialog(this);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Custom Dialog");
    
    
    Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
    dialog_btn.setOnClickListener(new View.OnClickListener() 
    {
        // Perform button logic
    }
    

    this is important:

    dialog_btn.setOnClickListener(new View.OnClickListener() 
    

    View.OnClcikListener

    Hope this helps.