Android: Opening a ContextMenu from onLongPress in custom WebView

12.5k views Asked by At

I'm currently trying to get a custom WebView that displays a ContextMenu when it is pressed for a longer time. As the default WebView class only displays a ContextMenu when a link is longPressed, I wrote my own class to override this behaviour:

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 the longPress is detected and the onLongPress method is called, however I am at a loss when it comes to displaying the ContextMenu. I tried doing it the usual way in my Activity:

@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 the MyWebView in the emulator, nothing happens. What do I have to call from onLongPress() to display the ContextMenu?

4

There are 4 answers

0
gngr44 On BEST ANSWER

Call Activity.openContextMenu(View v) in onLongPress. This would mean having the MyWebView keep a reference to the Activity though.

3
jbaiter On

I got it working now, building on gngr44's suggestion. I made my activity implement the OnLongClickListener class and provided a onLongClick() method that opens the context menu.

The revised code:

The custom 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);
        }
    };
}

My Activity:

@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);
}
0
Elad Avron On

I noticed that long pressing anything in the emulator requires a LOT of pressing, like 5-7 seconds as opposed to a regular 1-2 in real life. Make sure you press for at least 10 seconds, otherwise it would seem like nothing happens.

1
thedjinni On

Instead of accessing the activity from your view I would recommend using an Interface in your View and implementing that interface from your Activity.

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:
    }
}