jul
jul

Reputation: 37484

onTouch on a view not triggered when touching its ScrollView child

I can't get onTouch event triggered on a container when clicking on its ScrollView child.

I'm using API 12 on a Galaxy tab 10.1.

Anybody can help?

Thanks

Activity

public class TestActivity extends Activity{

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.dude);

        LinearLayout mylayout = (LinearLayout) findViewById(R.id.mylayout);        

        mylayout.setOnTouchListener(new OnTouchListener () {
            public boolean onTouch(View view, MotionEvent event) {

                // Only called when touched outside the ScrollView

                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    /* do stuff */
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    /* do stuff */
                }
                return true;
            }
        });        
    }
}

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mylayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:gravity="center"
        android:text="Touch here trigger parent&apos;s onTouch"
        android:textColor="#000000"
        android:textSize="40sp" />

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="1000dp"
                android:background="#b00000"
                android:gravity="center"
                android:text="Touch here DOES NOT trigger parent&apos;s onTouch"
                android:textColor="#000000"
                android:textSize="40sp" />
        </RelativeLayout>
    </ScrollView>

</LinearLayout>

enter image description here

Upvotes: 2

Views: 1885

Answers (3)

Shubham Goel
Shubham Goel

Reputation: 2186

You probably need this code

@Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
onTouchEvent(ev);
return false;
}

Upvotes: 0

kurayami88
kurayami88

Reputation: 121

mylayout.setOnTouchListener(new OnTouchListener () {
            public boolean onTouch(View view, MotionEvent event) {

                // Only called when touched outside the ScrollView

                if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {
                    /* do stuff */
                    return true;
                } else if (event.getAction() == android.view.MotionEvent.ACTION_UP) {
                    /* do stuff */
                    return true;
                }
                return false;
            }
        });

this should work... but you no longer have auto-scroll when you fling it hard... it'll be a sticky scroll moving only as much as you drag.

Upvotes: 1

Thkru
Thkru

Reputation: 4199

Did u try to create ONE OnClickListener and add it to all childs? Maybe this could solve your As the ScrollView makes it childs scrollable it wouldn't have an own area to click in. (Correct me if I'm wrong ^^)

Upvotes: 0

Related Questions