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.

skynet
  • 9,898
  • 5
  • 43
  • 52
Trey Balut
  • 1,355
  • 3
  • 19
  • 39

5 Answers5

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

Chris
  • 22,923
  • 4
  • 56
  • 50
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.

just me
  • 340
  • 2
  • 9
  • 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. – CorayThan Jan 26 '17 at 20:24
  • @CorayThan 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) – just me Jan 26 '17 at 21:49
2

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

Ryan Muller
  • 708
  • 7
  • 7
0

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

Azizi Musa
  • 1,009
  • 2
  • 10
  • 31
0

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

Samuel Moshie
  • 570
  • 1
  • 5
  • 20