How to do something after user clicks on my EditText

fhucho picture fhucho · Jan 22, 2010 · Viewed 58k times · Source

I have an EditText that shows time. After user clicks the EditText I want to show a TimePickerDialog, so I set a View.OnClickListener to my EditText.

But the OnClickListener is behaving weirdly - I touch the EditText and then software keyboard appears (which I don't want). When I touch again, OnClickListener.onClick() is finally called and the dialog appears.

What should I do if I want the dialog to appear immediately?

Answer

Kai picture Kai · Feb 17, 2010

Unlike most other controls, EditTexts are focusable while the system is in 'touch mode'. The first click event focuses the control, while the second click event actually fires the OnClickListener. If you disable touch-mode focus with the android:focusableInTouchMode View attribute, the OnClickListener should fire as expected.

<EditText
        android:text="@+id/EditText01"
        android:id="@+id/EditText01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusableInTouchMode="false" />