Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android EditText setOnClickListener

I have an EditText field. When I set onClickListener on it, it will require first the focus on the field and then the click to call the listener. So it's actually two clicks to call the listener. How can I fix this to work from the first click? I don't want to set focusable to false because then the program won't work.

like image 236
MikkoP Avatar asked Oct 16 '25 19:10

MikkoP


1 Answers

As others have said, the first touch focuses the view, the second touch "clicks" it. Instead of implementing OnClickListener, implement OnFocusChangeListener. e.g.

EditText edittext = (EditText)findViewById(R.id.myedittext);
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if(hasFocus) {
            //handle your situation here
        }
    }
});
like image 88
Kevin Coppock Avatar answered Oct 19 '25 09:10

Kevin Coppock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!