tags:

views:

932

answers:

4

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 wierdly - 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?

+1  A: 

It sounds like you don't want the user to actually be able to type in the EditText. You just want them to be able to pick a time via a time picker. So why not just a button that pops up a TimePickerDialog? You could display the time that was picked in a TextView.

Or you could just replace the EditText view with a TimePicker view (not a dialog, just a regular view).

mbaird
I want to use EditText only because it looks much nicer in my app than a button. I know I could achieve the same effect by using a button with appropriate drawables but just using edittext is simpler (I don't have to create the drawables etc.)
fhucho
Just a note... you wouldn't have to really create any drawables, just tell the button to use the EditText's drawables.
fiXedd
In this case, try android:editable="false" on your EditText view. You should still be able to capture onClick events, but since it's not editable the soft keyboard shouldn't pop up.
mbaird
Using Button with custom drawable and a few xml attributes was very simple after all, thanks for help.
fhucho
A: 

If I understand correctly you just need something like

<EditText android:text="@+id/EditText01" android:id="@+id/EditText01"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:clickable="true" android:inputType="none" />

not editable and clickable. Set the OnClickListener and you're done. In theory, in the practice you should add too

android:editable="false" 

which is deprecated but does the trick.

dtmilano
Unfortunately this doesn't work...
fhucho
Worked for me. What's the problem ?
dtmilano
With your code the keyboard doesn't open but it still calls my onClick() on the second click, not on the first. I tested it on Android 2.0.1 and my app has minSdk 3.
fhucho
A: 

I solved this by using a customized Button like this:

<Button android:id="@+id/btTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="test" android:textSize="20dp"
android:background="@android:drawable/edit_text" />
fhucho
+1  A: 

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" />
Kai