Android click listener on View work in strange manner

48 views Asked by At

I have the following layout.xml containing one ScrollView with some elements(picture and text)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/clickable"
            android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_weight="1"
                android:layout_gravity="center"
                android:src="@drawable/crown" />

            <TextView
                android:id="@+id/n1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="10dp"
                android:text="some text" />

        </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true">

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    android:src="@drawable/medal" />

                <TextView
                    android:id="@+id/n2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:padding="10dp"
                    android:text="some text" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true">

                <ImageView
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    android:src="@drawable/star" />

                <TextView
                    android:id="@+id/n3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:padding="10dp"
                    android:text="some text" />
            </LinearLayout>
        </LinearLayout>
</ScrollView>
</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity{

LayoutInflater inflater;
RelativeLayout MyLayout;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflater = LayoutInflater.from(this);
        MyLayout = (RelativeLayout) inflater.inflate(R.id.layout, null, false);
        MyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("TAG"," I'm clicked! ");
            }
        });

Click Listener above does not work when I click screen!!! But if I change code to this - everything is working

MyLayout.findViewById(R.id.clickable).setOnClickListener(new View.OnClickListener() {
     @Override
            public void onClick(View view) {
                Log.d("TAG"," I'm clicked! ");
            }
    });

So why should I set click listener to LinearLayout inside ScrollView inside parent RelativeLayout? Why click listener does not work for root xml element RelativeLayout???

2

There are 2 answers

1
Jonáš Tichý On

The problem is that your ScrollView is intercepting the touch events and not propagating them to parent layouts (in this case your RelativeLayout).

This thread might have some useful information about this topic.

1
Fahad Nasrullah On

Just add android:clickable="false" in your ScrollView. Then change your MainActivity like below:

public class MainActivity extends Activity {

LinearLayout myLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myLayout = (LinearLayout) findViewById(R.id.layout);
    myLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("TAG"," I'm clicked! ");
        }
    });
}

}

Note: activity_main.xml is xml file which you are loading.

Now your OnClick will work as expected.