Button on user dialog Does not respond to click events

I created a custom dialog that extends Dialog. One button on this dialog box is the OK button, which the user must click when finished entering information in other fields. I cannot get listeners to set this button to run.

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 tried to implement an implementation in which the Dialog class implemented 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): Not lucky yet.

 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 set breakpoints inside each of the listeners in both versions of the implementation, and the debugger never stops execution. I tried using inner classes for listeners who did not solve the problem.

Any clues? Thanks

+7
android
source share
4 answers

I found a solution here: Handling buttons in user dialogs

This works in my case.

+2
source share
 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 } 
+2
source share

Why am I not sure why, after the two examples mentioned in my post, it didn’t work, I understood how to make it work. I had to transfer the attachment of my listener to a button in the onStart () dialog method from the onCreate () dialog method.

This seems to be due to the fact that I also override the onStart () method in my custom dialog:

  public void onStart() { super.onStart(); setContentView(R.layout.heightdialog); ... } 

This code should “nullify” my listeners who were in the onCreate () method.

+1
source share

To intercept clicks on buttons, HeightDialog must implement View.OnClickListener

 public class HeightDialog extends Dialog implements View.OnClickListener { } 
0
source share

All Articles