Needing to double click EditText for click listener to respond
Asked Answered
L

5

19

I have a section of code where I want to change the text showing in a textView when the user selects an EditText box.

The problem I am having is that the textView only changes when I double click the EditText box, one click and there is no change to the textView.

Is there another click listener that I should be using?

final EditText box0105 = (EditText)findViewById(R.id.box0105);
final TextView txtHint = (TextView)findViewById(R.id.txtHint);
        box0105.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtHint.setText(onOneClick);                
        }
    });
Lomond answered 21/11, 2012 at 15:7 Comment(0)
S
12

Try setting OnTouchListener rather than OnClickListener

Stelliform answered 21/11, 2012 at 15:10 Comment(0)
M
28

Add android:focusableInTouchMode="false" in EditText xml then EditText will accept single click

Melanochroi answered 21/11, 2012 at 15:10 Comment(3)
focusableInTouchMode = "false" achieves the outcome, but it also removes the ability for the user to use the editText (I think?) Using the onTouchListener accepts single click but also maintains editability of the editTextLomond
@Lomond : no it will not removes the ability for user to use Edittext it just avoid first touch event.Regulator
How this answer got 20 upvotes I will never understand.Hyperextension
S
12

Try setting OnTouchListener rather than OnClickListener

Stelliform answered 21/11, 2012 at 15:10 Comment(0)
E
5

use this, it will fire very first.

editText.setOnTouchListener(new OnTouchListener() { 
        @Override 
        public boolean onTouch(View v, MotionEvent event) {
            if(MotionEvent.ACTION_UP == event.getAction())
                // Do whatever you want to do man, but don't trouble your mother
            return false; 
        } 

});
Expansion answered 2/11, 2017 at 8:33 Comment(0)
R
2

set your edittext in xml

<EditText
  android:focusable="false"
  ...
/>
Reyesreykjavik answered 25/11, 2017 at 2:33 Comment(0)
H
2

I set up 2 listeners one is OnClick and another one is OnFocusChange. Like this:

editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        System.out.println("Edittext has been clicked");
    }
});

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        System.out.println("Edittext has been clicked");
    }
});

OnFocus is called when the Edittext is clicked first time and then for all other times OnClick is called.

Hundred answered 16/2, 2021 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.