1

Today I've found strange behavior in my app. I've a view (actually it's TextView) with style as below:

> <style name="EditTextStyle.Default"
> parent="Base.V12.Widget.AppCompat.EditText">
>     <item name="android:textSize">@dimen/TEXT_SMALL_SIZE</item>
>     <item name="android:padding">@dimen/edit_text_default_padding</item>
>     <item name="android:layout_margin">@dimen/edit_text_default_padding</item>
>     <item name="android:textColor">@color/text.total.color</item>
>     <item name="android:background">@null</item>
>     <item name="android:focusable">true</item>
>     <item name="android:clickable">true</item>
>     <item name="android:focusableInTouchMode">true</item> </style>

I need to process onClick() actions and problem occurs only when you click this view at first time after onCreate() in other cases all works as expected. First click was skipped by android, additionally selector works well and view was highlighted, but onClick() method skipped first click.

I've solved this problem with no implementation this style, but maybe someone can explain me why or which setting broke me app?

Gorets
  • 2,434
  • 5
  • 27
  • 45
  • 1
    Have you tried removing focusable, clickable and focusableInTouchMode? If this is a text view, you shouldn't have to mess around with those flags. – Elli White Oct 20 '15 at 15:58

2 Answers2

5

The problem is with the following lines:

<item name="android:focusableInTouchMode">true</item>
<item name="android:focusable">true</item>

Because of these lines, the first click will be used up in setting the focus and then from second click onwards the onClick() will be called.

More info here: Android button - How to set focusable to true and still accept onClick listener on first click?

Community
  • 1
  • 1
Henry
  • 17,490
  • 7
  • 63
  • 98
1

If you set it from style just remove <item name="android:focusableInTouchMode">true</item>.

Or if u set it from xml just remove android:focusableInTouchMode="true"

L.Petrosyan
  • 430
  • 4
  • 12