Robby Smet Robby Smet - 1 month ago 20
Android Question

OnClick event only works second time on edittext

I have an edittext, and when the user clicks this edittext I want to show an alertdialog.

My code is the following :

edt.setInputType(InputType.TYPE_NULL);
edt.setFocusableInTouchMode(true);
edt.requestFocus();

edt.setCursorVisible(false);

edt.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
CommentDialog.buildDialog(mContext, identifier, false, edt.getId());
}
});


I don't want the keyboard to show up when the user clicks the edittext, so I set the inputtype to TYPE_NULL.

But when the edittext doesn't have focus and I click it, the onClick event isn't executed. When I click it a second time, the alertdialog shows up correctly.

How do I fix this?

Answer

Simply try to add this to your XML file. Your keyboard pops up when widget gains focus. So to prevent this behaviour set focusable to false. Then normal use OnClickListener.

<EditText
  android:focusable="false"
  ...
/>

Now, it should works.

Comments