9

I would like to add a GestureDetector to all views (view groups) of an activity without assigning it manually to every single view. Right now onFling() is only activated when swiping over the background but not when swiping on e.g. button1.

package com.app.example;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class ExampleActivity extends Activity {
    Context mContext = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;

        LinearLayout parent = new LinearLayout(mContext);
        parent.setOrientation(LinearLayout.VERTICAL);
        parent.setBackgroundColor(Color.RED);

        Button button1 = new Button(mContext);
        button1.setText("Button 1");
        Button button2 = new Button(mContext);
        button2.setText("Button 2");

        parent.addView(button1);
        parent.addView(button2);

        final GestureDetector gestureDetector = new GestureDetector(
                new MyGestureDetector());

        parent.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDetector.onTouchEvent(event);

                return true;
            }
        });

        setContentView(parent);
    }
}

class MyGestureDetector extends SimpleOnGestureListener {

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2,
            float velocityX, float velocityY) {

        // right to left
        if(e1.getX() - e2.getX() > 10 && Math.abs(velocityX) > 20) {
            Log.i("onFling", "right to left");

            return false;

        // left to right
        }  else if (e2.getX() - e1.getX() > 10 && Math.abs(velocityX) > 20) {
            Log.i("onFling", "left to right");

            return false; 
        }

        return false;
    }

}
Goo
  • 1,318
  • 1
  • 13
  • 31
Paradiesstaub
  • 2,590
  • 2
  • 18
  • 28

1 Answers1

2

You can use a GestureOverlayView see a previus stackoverflow post on how to use it.

Community
  • 1
  • 1
AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • 1
    Thanks for the GestureOverlayView tip. It took me a while to get how to use it -> http://www.vogella.com/articles/AndroidGestures/article.html – Paradiesstaub Jul 09 '12 at 15:47
  • 2
    I would like to know how to do that without using the GestureOverlayView. – halxinate Mar 06 '13 at 21:59
  • 1
    @halxinate If you do not want to use the GestureOvelayView, the only other way I can think of, is assigning a gesture detector to all views as the question states. However, i believe this may have some limitations since the gesture must be performed on the surface of the view without exceeding it (I have not tested it yet, so this may not be the case). – AggelosK Mar 07 '13 at 07:51