Android: opening ContextMenu from onLongPress in a custom WebView

I am currently trying to get a custom WebView that displays ContextMenu when it is clicked for a longer time. Since the default WebView class displays ContextMenu when the link is longPressed, I wrote my own class to override this behavior:

public class MyWebView extends WebView {
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

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

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            // The ContextMenu should probably be called here
        }
    };
}

This works without problems, and longPress is detected, and the onLongPress method is called, however I don’t understand when this happens when you show ContextMenu. I tried to do this in the usual way in my work:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}

However, when I longPress MyWebView in the emulator, nothing happens. What do I need to call from onLongPress () to display ContextMenu?

+5
4

Activity.openContextMenu( v) onLongPress. , MyWebView , .

+1

, gngr44. OnLongClickListener onLongClick(), .

:

WebView:

public class MyWebView extends WebView {
    MyActivity theListener;
    Context context;
    GestureDetector gd;

    public MyWebView(Context context, AttributeSet attributes) {
        super(context, attributes);
        this.context = context;
        gd = new GestureDetector(context, sogl);
    }

    // This is new
    public void setListener(MyActivity l) {
        theListener = l;
    }

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

    GestureDetector.SimpleOnGestureListener sogl =
                new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent event) {
            return true;
        }

        public void onLongPress(MotionEvent event) {
            theListener.onLongClick(MyWebView.this);
        }
    };
}

:

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

    MyWebView mwv = (MyWebView) findViewById(R.id.mwv);
    registerForContextMenu(mwv);
}

public boolean onLongClick(View v) {
    openContextMenu(v);
    return true;
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.context, menu);
}
+3

.

public class MyWebView extends WebView {
    private OnLongPressListener mListener;

    public MyWebView(Context context, AttributeSet attributes) {
        mListener = (OnLongPressListener) context;
    }

    public void onLongPress(MotionEvent event) {
        mListener.onLongPress(your variables);
    }

    public interface OnLongPressListener {
        public void onLongPress(your variables);
    }
}

public class YourActivity extends Activity implements OnLongPressListener {

    @Override
    public void onLongPress(your variables) {
        // handle the longPress in your activity here:
    }
}
+2

I noticed that a long press on something in the emulator requires a lot of clicks, for example 5-7 seconds, unlike the usual 1-2 in real life. Make sure you press for at least 10 seconds, otherwise nothing will happen.

0
source

All Articles