mardi 12 mai 2015

How to set onLongPress for a particular relative layout using GestureDetectorCompat?

In my code, onLongPress works in full screen but I need onLongPress into particular relative layout.After getting X and Y values in LongPress, I have to perform some actions in onLongPress event.

public class MainActivity extends ActionBarActivity  {

private GestureDetectorCompat mDetector;
public Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDetector = new GestureDetectorCompat(this, new MyGestureListener());
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myView);
}

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

public void message(Context ctx,String msg,int duration) {
    Toast.makeText(ctx, msg, duration).show();
}

private class MyGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
    public void onLongPress(MotionEvent e) {
        float x = e.getX();
        float y = e.getY();

        message(getBaseContext()," X value is " + x + " Y value is "+ y ,Toast.LENGTH_SHORT);
        super.onLongPress(e);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        message(getBaseContext(),"Single tab" ,Toast.LENGTH_SHORT);
        return super.onSingleTapConfirmed(e);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire