In my Android app I need to provide some custom actions on the contextual action menu after the user selects some text. To do so, I capture long clicks and start the action mode, providing a custom ActionMode.Callback (as pointed out in Android Docs). The problem is that this way the selection text cursors won't show up. So, is there any way to activate the selection text mode programmatically??
public void SelectText(){
try{
KeyEvent shiftPressEvent =
new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mWebView);
}catch(Exception e){
throw new AssertionError(e);
}
}
In this case we are activating text select on mWebView
Use startActionMode().
See How to invoke the ActionBar's ContextMenu-like behavior?
Related
I am using android 4.x .As I select the data from webview , there
comes a option menu "copy" to copy the data on clipboard.
i wanna create own button .as i click on that button it will copy the
text on clipboard. i am able to copy selected text from webview to
clipboard till 2.3 version.
but it's not happening in android 3.0 or above.
It's measure problem.i have waste more time for it.Plz help me
My code is
public void SelectText(){
try{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mainWebView);
}catch(Exception e){
throw new AssertionError(e);
}
}
I've create a dialog with a multiline edittext-field. The problem is that the [ENTER]-key of the soft keyboard is closing the keyboard instead of creating a new line. With imeOptions, it's possible to configure a lot, but not a newline-command... How can I accomplish this?
Building for a Galaxy Tab 2 with Android 4.0.3.
I found out that setting the raw inputtype of the EditText to multiline is working where the "normal" input type isn't.
final EditText remark = new EditText(MyClass.this);
remark.setRawInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
This did it for me.
You might be able to accomplish this by creating a new TextWatcher. Register this textwatcher to your EditText and implement a breakline when receiveing the return key.
EDIT:
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
For example:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true
default:
return super.onKeyUp(keyCode, event);
}
}
Source:
Android :
http://developer.android.com/training/keyboard-input/commands.html
A list of the KeyEvents:
http://developer.android.com/reference/android/view/KeyEvent.html
I am working on Android 4.0.3 . I am trying to select the text from the WebView and saving it as a string . I have tried this Code:
public void SelectText(){
try{
KeyEvent shiftPressEvent =
new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(mWebView);
}catch(Exception e){
throw new AssertionError(e);
}
But it doesnot work . In Android 4 there is already an inbuilt menu for selecting text but how this menu could be overridden ??
This solution uses a javascript interface to pass touches to the page and effectively cut Android's native selection out of the equation. This has been tested from 2.2 to 4.0.3.
Look at this Github Project .
I followed these two exemples of developer.android (Creating an Input Method, Soft Keyboard sample). Think everything is correct, but the custom keyboard dont shows up.
Sorry but i dont understand the code, how i call this new keyboard?
thanks for all.
You can open the "Change input method" menu programatically like this:
InputMethodManager mgr =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (mgr != null) {
mgr.showInputMethodPicker();
}
You may also want to open "Language & Input settings" so your users can enable your input method. You can do that like this:
startActivityForResult(
new Intent(android.provider.Settings.ACTION_INPUT_METHOD_SETTINGS), 0);
You need to select the new keyboard to be your default. You have to turn it on in the device settings first, and then you need to long press inside of an EditText to be able to get the InputOptions menu. This will allow you to select your keyboard
I want to select Text from Webview And Then Highlight only selected apparition of that word. someone has an idea? with javascript does not work because getSelection function return nothing on android webview.
Please help.
As far as I can understand, you can't select text on an Android touch device like you can on a desktop browser (with your mouse).
It seems like the way text is selected on android device only can be copied into the Clipboard or used by the native "Share" app.
Selecting the text => copy it into Clipboard => use the ClipboardManager to fetch it back.
I might is not as simple and smooth as you was hoping for, but maybe it will work out for you :)
Once you have the value of the selected text, your can always pass it to a javascript function that search/replace for it in your html and highlights it, by adding/padding some html to it.
Link to ClipboardManager.
http://developer.android.com/reference/android/text/ClipboardManager.html
Here's a demo of fetching selected text with your mouse on a regular desktop browser:
http://bit.ly/AwggX9
Try running the same with your Android device. The text selection doesn't work the same way. I guess its the Share/Clipboard manager that handles the selection, and not the browser itself.
Good luck, hope you find a solution that works, and will be happy to hear about it.
Have you looked into:
mainpage.showFindDialog("Alice", true);
where mainpage is the WebView...
This will popup above the webview a bar where the text is entered - and then highlight the first occurrence of the text. There are arrows to jump to the next, etc...
Unless you mean you wish to highlight certain words - and have them stay that way - then you need to inject some javascript to do a highlighting function.
The following code will copy your text to clipboard then from clipboard fetch the text and search it..
private void emulateShiftHeld(WebView view)
{
try
{
KeyEvent shiftPressEvent = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0);
shiftPressEvent.dispatch(view);
}
catch (Exception e)
{
Log.e("dd", "Exception in emulateShiftHeld()", e);
}
}
The following code will highlight you the text you have selected...
ClipboardManager ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipMan.setText(ClipMan.getText().toString());
wb.findAll(ClipMan.getText().toString());
try
{
Method m = WebView.class.getMethod("setFindIsUp", Boolean.TYPE);
m.invoke(wb, true);
}
catch (Throwable ignored){}