Search code examples
javaandroidinterfacegesturedetector

How to create an interface from a class


I am new to Android Programming/ Java I wanted to know how I should add gesture Directions when I already have a class extended to MainActvity?

    package #####app.myapplication;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.MotionEvent;
import android.view.GestureDetector;
import android.support.v4.view.GestureDetectorCompat;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{

    private GestureDetectorCompat mygesture;
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;

    @Override
    public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent motionEvent) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent motionEvent) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent motionEvent) {

    }



       @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
            }
        @Override
        public void onPointerCaptureChanged(boolean hasCapture) {

        }


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

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Context context = getApplicationContext();
            CharSequence text = "Welcome to ####";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            WebView vedant = (WebView) findViewById(R.id.vedant);
            vedant.loadUrl("http://##.ml");
            WebSettings webSettings = vedant.getSettings();
            webSettings.setJavaScriptEnabled(true);
            vedant.setWebViewClient(new WebViewClient());
            this.mygesture = new GestureDetectorCompat(this,this);
            mygesture.setOnDoubleTapListener(this);



        }
    }

I know that to use Gesture Direction I need to :

@Override
    public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
        float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH){
                    return false;
                }
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onLeftSwipe();
                }
                // left to right swipe
                else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                        && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    onRightSwipe();
                }
            } catch (Exception e) {

            }
            return false;
        }
        return false;
    }

But for which I need to extend another class SimpleOnGestureListener. As i could not extend to the SimpleOnGestureListener class I thought i could create a interface and then implement it. Is that possible. If so how?


Solution

  • If I understand correctly, you have a GestureDetectorCompat with 2 parameters:

    • The Context
    • a listener (GestureDetector.OnGestureListener)

    At the moment you do mygesture = new GestureDetectorCompat(this,this); where this is your Activity. But why do you want your Activity to be the listener? You can create a listener in a separated class.

    So you can do:

    MainActivity

    this.mygesture = new GestureDetectorCompat(this, new MyGestureListener());
    

    MyGestureListener

    public class MyGestureListener extends SimpleOnGestureListener {
    
        @Override
        public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
           // ... 
           // Your code
        }
    }