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) {
}
};
}
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?