Search code examples
androidandroid-layoutandroid-linearlayoutandroid-coordinatorlayoutandroid-nestedscrollview

First click on a View is not working in Android


I have a LinearLayout which needs to be clickable inside a NestedScrollView inside a CoordinatorLayout and almost all the time the first time I click it it simply doesn't work, I must click it another time.

Click error

Clickable LinearLayout:

<LinearLayout
            android:id="@+id/qr_code_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:clickable="true"
            android:focusable="true"
            android:background="?android:attr/selectableItemBackground"
            tools:ignore="UseCompoundDrawables">

EDIT: Okay this is very weird, if I wait some seconds before trying to click the View, it works the first time. It only fails if I click it just after scrolling to the bottom.


Solution

  • I had the same problem. Inside the NestedScollView there was a RelativeLayout element needed to be clicked. As long I didn't scroll to the very bottom, the element could be clicked with a single click. If I would reach the bottom I needed two clicks, except if I waited a number of seconds.

    The problem proved to be on the default layout_behavior of the AppBarLayout. I used the custom one suggested in the post below and the problem was solved.

    onClick method not working properly after NestedScrollView scrolled