Reputation:
How can make an EditText have a onClick event so that on single click an action is done.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
this is not working as excepted....single click gives just the onscreen keypad but not the datepicker dialog which appears only if i double click
Upvotes: 4
Views: 6026
Reputation: 5150
if we just add android:focusableInTouchMode="false"
in edittext on layout page it should work in a singleclick on its onclicklistener. no need to handle onFocusChangeListener.
Upvotes: 11
Reputation: 86948
Rewrite
I have an EditText that launches a dialog when the user either clicks it once or navigates to it with a trackball / directional pad. I use this approach:
I have also tried this (however I now remember this method did respond to trackball movement):
setFocusable(false)
to prevent user input.Hope that helps.
Upvotes: 0
Reputation: 34301
EditText
is not meant for singleClick
.
I mean you should not use Click Listener with it. rather you can do like,
Use onFocusChangeListener
which is also not 100% correct approach.
Best would be instead the EditText
use one TextView
write onClick
of that and if needed give a background image to that TextView.
Upvotes: 0
Reputation: 2602
Change your code from an onClickListener to an OnFocusChangeListener.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
showDialog(DATE_DIALOG_ID);
}
}
});
}
Upvotes: 0