scrollview2 = (ScrollView) findViewById(R.id.scrollview2); scrollview2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { gonext(); }}); 

Property clickable = true , but when you click anywhere on the ScrollView nothing happens. Why?

I tried to hang the same code on any of the Layouts , but compared to ScrollView take up much less space on the screen, and I need to make some sort of any key.

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" android:id="@+id/main_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="appname.body" android:paddingBottom="6dp" android:background="@drawable/body1" android:clickable="true" android:contextClickable="true"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:animationCache="true" android:fadeScrollbars="true" android:id="@+id/scrollview2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:clipToPadding="false" android:layout_alignParentBottom="true" android:layout_marginTop="60dp" android:clickable="true"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linear123" android:clickable="true"> <TextView android:text="TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="19dp" android:layout_marginStart="19dp" android:layout_marginTop="39dp" android:id="@+id/textView2" /> </LinearLayout> </ScrollView> </RelativeLayout> 
  • Show the layout file. - post_zeew
  • Stackoverflow for some reason removes the main (parent?) RelativeLayout. - FullyRetarded
  • The code is distinguished by 4 spaces. If it does not work out - leave it as it is, the code will be contained in the message, but it may not be displayed, I will format it myself. - post_zeew
  • Like flooded. Take a look? - FullyRetarded
  • See the answer. - post_zeew

1 answer 1

Property clickable = true , but when you click anywhere on the ScrollView nothing happens. Why?

Because clicks on ScrollView processed by its children.

To handle clicks on a ScrollView you need to add any ViewGroup , for example, like you have a LinearLayout , stretch it over the entire ScrollView area, set the android:clickable="true" attribute android:clickable="true" and process clicks on this ViewGroup .

In order for your LinearLayout stretch across the entire ScrollView , ScrollView need to add the android:fillViewport="true" attribute to the ScrollView android:fillViewport="true" .