user1381180
user1381180

Reputation:

Single click edittext

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

Answers (4)

Ranjit
Ranjit

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

Sam
Sam

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:

  • Use an OnFocusChangeListener for gaining focus to open the dialog.
  • Override dismissDialog() to clear the focus from the EditText when the user closes the dialog, preventing the user from entering text without the dialog (as far as I can tell) .

I have also tried this (however I now remember this method did respond to trackball movement):

  • Use an OnClickListener for touch events.
  • Set setFocusable(false) to prevent user input.

Hope that helps.

Upvotes: 0

MKJParekh
MKJParekh

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.


enter image description here

Upvotes: 0

Scott
Scott

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

Related Questions