1

I have very simple activity: just TextView inside ScrollView.

I want to handle user clicks and longclicks on whole ScrollView to start some action. I can easily handle click events on TextView, but cannot on ScrollView.

When text content is long enough it's not a problem, but when it's just one line of text than user needs to click that particular line (top of the screen) which is not very usefull.

How to detect click events in ScrollView? (or any other solution)

Sample activity:

public class TestActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);
}

public void testOnClick(View view) {
    Log.d("TestActivity", "Hello? Anybody there?");
    finish();
}}

And layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff0000"
    android:clickable="true"
    android:onClick="testOnClick">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="test text\n test text\n test text" />
</ScrollView>

sZpak
  • 247
  • 10

2 Answers2

0

As mentioned in this post you should set clickable false to the other elements within the scroll view for it to work OnClickListener on scrollView

Cheers

Community
  • 1
  • 1
wnieves19
  • 550
  • 4
  • 16
0

What you want is to having a bigger click target for your TextView.

Just set the click / long click listener on the TextView. You can add a padding to increase the size of the view like this:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:text="test text\n test text\n test text" />

This ensures a click target of at least 40dp, depending on your font size, which is easy to click on.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134