I need to provide options as shown in below image when user select text from webview.
Which method should I override to provide options?
Copy functionality in a WebView
is available by default in Android 3.0 and above. To enable copy in version below 3.0, there is a little code that has to be added. The first thing I did was add a Copy button to my Actionbar
at the bottom of the view.
Check out the Copy Text in a WebView in Android
Below is the code to show how to use:
public class TryDemoActivity extends Activity implements
OnClickListener {
private WebView webview1;
private Button copyButton;
private Button pasteButton;
private Button highlightButton;
private ClipboardManager clipboard;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview1 = (WebView) findViewById(R.id.webview);
copyButton = (Button) findViewById(R.id.copy);
pasteButton = (Button) findViewById(R.id.paste);
webview1.loadUrl("file:///android_asset/1.1.html");
clipboard =(ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
copyButton.setOnClickListener(this);
pasteButton.setOnClickListener(this);
}
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
registerForContextMenu(view);
Toast.makeText(this, "select_text_now", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.copy:
emulateShiftHeld(webview1);
break;
case R.id.paste:
Toast.makeText(this, clipboard.getText(),Toast.LENGTH_SHORT).show();
break;
}
}
}
EDITED:
Note : The code emulateShiftHeld which is deprecated in 4.0, which is probably why it is working in 2.2 and not in 4.0.