Android - Gesture Detection (Swipe up/down) on particular view
Asked Answered
G

3

30

I am trying to implement the OnGestureListener in Android.
I have three TextViews in my layout.
What i am trying to achieve is to set Gesture Listener for two of the textViews .
Here is the layout -

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvOne"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="One" />

    <TextView
        android:id="@+id/tvTwo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvOne"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Two" />

    <TextView
        android:id="@+id/tvThree"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTwo"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:text="Three" />

</RelativeLayout>

And here is the activity

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.GestureDetector;
    import android.view.GestureDetector.OnGestureListener;
    import android.view.MotionEvent;

    public class TimeActivity extends Activity implements OnGestureListener {

    GestureDetector gestureScanner;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wheelview);
        gestureScanner = new GestureDetector(this);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return gestureScanner.onTouchEvent(event);
    }

    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        // TODO Auto-generated method stub
        Log.i("Test", "On Fling");
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}

At present Fling for all the three textviews is getting called .
Is there any way through which i can set the Gesture Listener for some particular views in the layout.
Any help will be highly appreciated.

Gordan answered 18/2, 2013 at 7:42 Comment(0)
E
30

Do this in your onCreate method.

findViewById(R.id.tvOne).setOnTouchListener(new View.OnTouchListener() { 
            @Override
           public boolean onTouch(View v, MotionEvent event){
                return gestureScanner.onTouchEvent(event);
           }
  });
Ekg answered 18/2, 2013 at 7:52 Comment(7)
Thanks for the Answer. Both of the answers are correct. I can not accept both the answers , I am up voting you both.Gordan
How to set onLongPress for particular layout(Relative).Coahuila
@reegan29, the same way. One of the override methods on a GestureDetector is onLongPress()Cimbri
How can we know which view was flung?Contradiction
@SrujanBarai In above case View with id R.id.tvOne has listener so thats the one being touched. Or you can use the View passed in onTouch()Ekg
Oh yhh that is obvious but the onFling method does not have a view parameter.Contradiction
How can i use ACTION_MOVE using this gestureScannerEnchilada
E
11

You can set OnTouchListeners to individual TextViews.

findViewById(R.id.tvOne).setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
         // Your code here
    }
}
Extensive answered 18/2, 2013 at 7:47 Comment(0)
P
2

A suggestion

If you don't want to detect all gesture,try this class: SimpleGestureListener created by myself.

Now here are some snippets for this class's usage.

Usage

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private GestureDetector mDetector;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SimpleGestureListener simpleGestureListener = new SimpleGestureListener();
        simpleGestureListener.setListener(new SimpleGestureListener.Listener() {
            @Override
            public void onScrollHorizontal(float dx) {
                Log.i(TAG,"horizontal = " +dx);
            }

            @Override
            public void onScrollVertical(float dy) {
                Log.i(TAG,"vertical = " +dy);
            }
        });
        mDetector = new GestureDetector(this, simpleGestureListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }
}

It can be used for detecting swiping gesture :

  • left or right
  • up or down
Presentationism answered 21/2, 2017 at 9:11 Comment(1)
Please make you interface "Listener" public in your github project , ThanksMaurili

© 2022 - 2024 — McMap. All rights reserved.