I’m making an dialog box on my EditText, so when I touch the EditText, the box will show (setOntouchListener). The problem is: when I touch the EditText, the box is shown twice (when I click dismiss button in the box, the second box shows and the content of the box is exactly the same)
FoodText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.customfoodinput);
dialog.setTitle("Insert your Food");
spinner1 = (Spinner) dialog.findViewById(R.id.spinner_foodTime);
spinner2 = (Spinner) dialog.findViewById(R.id.spinner_foodType);
foodtimeArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(foodtimeArray);
foodtypeArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(foodtypeArray);
Button btntampung = (Button) dialog.findViewById(R.id.btn_inputfood);
btntampung.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Object item = spinner1.getSelectedItem();
String dataspinner = item.toString();
Object item2 = spinner2.getSelectedItem();
String dataspinner2 = item2.toString();
dialog.dismiss();
FoodText.setText(dataspinner + "; " + dataspinner2);
}
});
dialog.show();
return false;
}
});
Anyone could help me?
The problem is that a touch event detects more interaction than what you want. It detects the press and the release, so it is called two times. You want to detect only the press you can check the type of the event in your touchListener using an if (
if(arg1.getAction(); == MotionEvent.ACTION_DOWN)):If you want a simpler solution you can replace your
onTouchListenerfor anonClickListener.Instead of setting
setOnTouchListener(new OnTouchListener()and overridingpublic boolean onTouch(View arg0, MotionEvent arg1)replace those two line of code by:If you want to ignore the keyboard try to disable it using this question.