i want an EditText which creates a DatePicker when is pressed. So i write the next code:
mEditInit = (EditText) findViewById(R.id.date_init);
mEditInit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATEINIT_DIALOG);
}
});
But when i press the EditText the action is the typical: a cursor waiting for typing text instead show the Dialog i want.
Any idea?
Thanks
Normally, you want maximum compatibility with
EditText
's normal behaviour.So you should not use
android:focusable="false"
as, yes, the view will just not be focusable anymore which looks bad. The background drawable will not show its "pressed" state anymore, for example.What you should do instead is the following:
By setting the input type to
TYPE_NULL
, you prevent the software keyboard from popping up.By setting the
OnClickListener
andOnFocusChangeListener
, you make sure that your dialog will always open when the user clicks into theEditText
field, both when it gains focus (first click) and on subsequent clicks.Just setting
android:inputType="none"
orsetInputType(InputType.TYPE_NULL)
is not always enough. For some devices, you should setandroid:editable="false"
in XML as well, although it is deprecated. If it does not work anymore, it will just be ignored (as all XML attributes that are not supported).Default working of
EditText
: On first click it focuses and on second click it handlesonClickListener
so you need to disable focus. Then on first click theonClickListener
will handle.To do that you need to add this
android:focusableInTouchMode="false"
attribute to yourEditText
. That's it!Something like this:
Here is the solution I implemented
OR
See the differences by yourself. Problem is since (like RickNotFred said) TextView to display the date & edit via the DatePicker. TextEdit is not used for its primary purpose. If you want the DatePicker to re-pop up, you need to input delete (1st case) or de focus (2nd case).
Ray
Why did not anyone mention
setOnTouchListener
? UsingsetOnTouchListener
is easy and all right, and just return true if the listener has consumed the event, false otherwise.IMHO I disagree with RickNotFred's statement:
Displaying a dialog to edit the date when the use presses the an EditText is very similar to the default, which is to display a keyboard or a numeric key pad. The fact that the date is displayed with the EditText signals to the user that the date may be changed. Displaying the date as a non-editable TextView signals to the user that the date may not be changed.
Here is what worked for me
Set editable to false
/>
Then add an event listener for OnFocusChange