TextView onClick not responding to single tap
Asked Answered
S

5

16

I have a TextView with the following assignment code:

Answer1TextView = (TextView) findViewById(R.id.editText1);
Answer1TextView.setOnClickListener(answer1TextViewListener);

and here is my onClickListener:

private  OnClickListener answer1TextViewListener = new OnClickListener() 
{
    public void onClick(View v) 
    {
        if(Answer1Win){
            Toast.makeText(QuizScreen.this,"Correct ",2).show();
        } else
        {
            Toast.makeText(QuizScreen.this,"Incorrect, Pick Another Answer",2).show();
        }
    }
};  

My problem is the Toast only is displayed after a double tap. I cannot find a setting the drives this behavior, what could be set wrong to not display after a single tap.

Sumac answered 30/11, 2011 at 20:56 Comment(1)
Does it work for both Correct and Incorrect, even after 2 taps?Highbred
E
31

The first click just sets the focus to the TextBox then the second click actually gets handled as a click. Rather than using an onClickListener, you may have better luck with an onFocusChangeListener

Excursionist answered 30/11, 2011 at 21:4 Comment(6)
Alternatively, just set android:focusable="false" to the TextView in the XML, or call Answer1TextView.setFocusable(false);.Tighten
KC, .setFocusable(false); fixed the problem, thanks for the assist.Sumac
KC thanks for your suggestion. .setFocusable(false); worked for me. Great. Keep posting useful stuff. thanks againGalah
FYI Setting focusable to false will disable setError()Crackle
@Crackle then how to display errorMessage in focusable false TextView or EditText???Carrolcarroll
@Chirag Savsani: I solved it by calling 1. setFocusable(true); and 2. setFocusableInTouchMode(true); before calling setError method and in the xml do not set setFocusable neither focusableInTouchMode It worked with me, this way. Hope I answered you in a good manner.Whitecollar
S
3

As Chris said, the first tap focuses the TextView and the second tap clicks it.

Setting android:focusableInTouchMode="false" fixes the problem for touchscreens but without breaking functionality for non-touchscreen devices.

If you were to simply use android:focusable="false" that would prevent, for example, d-pad users from clicking the view at all.

Sparkman answered 27/11, 2013 at 8:49 Comment(2)
Looks to me like the d-pad isn't really relevant in today's world. I don't think focusable="false" will prevent functionality for any disabled users, as far as I can tell.Burg
@Burg Given that both attributes are equally easy to set, personally I'd rather choose the one that doesn't break functionality on certain devices - even if those devices are less common at the moment. Things change (e.g. android TVs might become more common) and assuming that your users are all touchscreen might well cause headaches in the future - and in this case there's no immediate benefit to it anyway. (disclaimer: I haven't kept up to date with Android so assuming the attributes still do what they used to when I wrote the answer)Sparkman
D
2

The issue may be that textIsSelectable is true. Set textIsSelectable="false" for the TextView in XML.

Despite answered 28/4, 2016 at 14:51 Comment(0)
N
0

The correct way to do that is android:clickable="true"

Netti answered 6/7, 2015 at 9:28 Comment(2)
TextView is clickable by default. This probably isn't the issue.Pearson
I have tried it as I got same problem. putting ^ that, solve my problem. My TextView only need 1 tap to respond.Netti
G
0

use OnTouchListener instead onFocusListener triggers twice when you enter and leaves the key

Geophysics answered 14/6, 2017 at 15:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.