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
I found a solution here: Handling buttons in custom dialogs
It works in my case.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With