Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button on Custom Dialog Not Responding to Click Events

Tags:

android

I created a custom dialog that extends Dialog. One button on that the dialog is an “OK” button which the user is expected to press when finished entering information in other fields. I cannot get any listeners set to that button to fire.

public class HeightDialog extends Dialog {

   private Button okButton;

   …

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.heightdialog);

      this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);

      this.okButton.setOnClickListener(new android.view.View.OnClickListener() {
         public void onClick(View v) {
            // Does not fire
            HeightDialog.this.dismiss();
            return;
         }
      });

      this.okButton.setOnLongClickListener(new OnLongClickListener() {
         public boolean onLongClick(View v) {
            // Does not fire
            HeightDialog.this.dismiss();
            return true;
         }
      });

     this.okButton.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
           // Does not fire
           HeightDialog.this.dismiss();
           return true;
        }
     });

   …
}

I also attempted an implementation where the Dialog class implemented the listeners(http://www.androidcompetencycenter.com/2009/01/android-basics-dialogs-and-floating-activities/) instead of using inner classes(http://about-android.blogspot.com/2010/02/create-custom-dialog.html): Still no luck.

public class HeightDialog extends Dialog implements View.OnClickListener {

   private Button okButton;

   …

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.heightdialog);

      this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);

      this.okButton.setOnClickListener(this);


   public void onClick(View view){
      HeightDialog.this.dismiss();
      return;
   }       
   …
}

I have set breakpoints inside each of the listeners in both versions of the implementation, and the debugger never stops execution. I have attempted to use inner classes for the listeners which did not solve the problem.

Any clues? Thanks

like image 475
Mike Avatar asked Oct 12 '10 12:10

Mike


2 Answers

I found a solution here: Handling buttons in custom dialogs

It works in my case.

like image 164
anticafe Avatar answered Sep 28 '22 15:09

anticafe


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
}
like image 42
Chirag Patel Avatar answered Sep 28 '22 13:09

Chirag Patel