Search code examples
androidandroid-layoutandroid-scrollview

Clickable LinearLayout inside ScrollView doesn't fire onClick


I have the following problem. On my layout, I have a ScrollView with fillViewport="true". Inside, I have a clickable LinearLayout (which in turn, has 4 views inside). It seems that my ScrollView intercepts the LinearLayout's onClick. My code is this:

<ScrollView
    android:id="@+id/signup_scrollview"
    android:layout_marginTop="56dp"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:clickable="false"
    android:fillViewport="true">
    ....
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar_actionbar">
        ....
        <LinearLayout
            android:id="@+id/add_product_button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="horizontal">
         ....
         </LinearLayout>
    </RelativeLayout>
</ScrollView>

I've tried to implement an onInterceptTouchEvent(MotionEvent e); on my ScrollView but to no avail. Can someone guide me as to how to fire my LinearLayout's onClick? Thanks.


Solution

  • Just write this in inside linear layout tag

      android:onClick="onClick"
    

    and inside your java code

      public void onClick(View v) {
    
        if(v.getId()==R.id.add_product_button){
             // do your code here
             System.out.println("Layout click");
        }
    }