tags:

views:

80

answers:

2

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

A: 

In order to intercept button clicks HeightDialog must implement View.OnClickListener

public class HeightDialog extends Dialog implements View.OnClickListener
{
}
barmaley
Having the Dialog implement the listener did not help. Updated the Question with both approaches used with the same result.
Mike
A: 

Why I am not sure why following the two examples mentioned in my post did not work, I figured out how to get it to work. I had to move the attachment of my listener to the button in the dialog's onStart() method from the dialog's onCreate() method.

It appear this is related to me also overriding the onStart() method in my custom dialog: public void onStart() { super.onStart(); setContentView(R.layout.heightdialog);

  ...

}

That code must have "zeroed" out my listeners which were in the onCreate() method.

Mike