0

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???

Vadim Yach
  • 11
  • 2

2 Answers2

0

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.

  • Hi Jonas! I tried to add id to ScrollView like I do to LinearLayout inside ScrollView android:id="@+id/clickable" , but it does not work. Means ScrollView does not listen to clicks by some unknown reason. The only solution working is as I wrote in the request. It looks like some Android bug-feature for me... Very strange! – Vadim Yach Oct 04 '20 at 06:51
0

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.

Fahad Nasrullah
  • 329
  • 1
  • 5
  • Hi Fahad! I did tried to set up android:clickable="false" everywhere on ScrollView and LinearLayouts , but it did not help to me. The only click listener working was only on LinearLayout inside my ScrollView... It looks very strange. – Vadim Yach Oct 04 '20 at 06:53