I am having a little issue when trying to use both onTouchListener and onCLickListener for the ok button within a dialog. Basically when I click on the OK button to dismiss the dialog, it recognises the onTouch but it doesn't perform the onClick where I want the dialog to close, the dialog remains open. What am I doing incorrectly in my implementation?
questionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog);
Button dialogButton = dialog.findViewById(R.id.dialog_button_OK);
// if button is clicked, close the custom dialog
dialogButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
setButtonPress(v, event);
return true;
}
});
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
public void setButtonPress(View v, MotionEvent event){
int sdk = android.os.Build.VERSION.SDK_INT;
Button view = (Button) v;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart_dark));
} else {
v.setBackground(getResources().getDrawable(R.drawable.love_heart_dark));
}
break;
case MotionEvent.ACTION_UP:
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
} else {
v.setBackground(getResources().getDrawable(R.drawable.love_heart));
}
break;
case MotionEvent.ACTION_CANCEL: {
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.love_heart));
} else {
v.setBackground(getResources().getDrawable(R.drawable.love_heart));
}
break;
}
}