Selecting text in a WebView? - android

The browser does this by calling public void emulateShiftHeld() method on the WebView which is hidden in the SDK.
Any other options?

From the class that extends WebView:
public void selectAndCopyText() {
try {
Method m = WebView.class.getMethod("emulateShiftHeld", null);
m.invoke(this, null);
} catch (Exception e) {
e.printStackTrace();
// fallback
KeyEvent shiftPressEvent = new KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(this);
}
}
And then you have to use ClipboardManager to watch for new text.
Works on Android 1.5 - 2.3. emulateShiftHeld() made public since 2.2.

This chunk of code does the exact same thing as emulateshiftheld(). It allows the user to select text. Then automatically copies it to the clipboard.
KeyEvent shiftPressEvent = new KeyEvent(0,0,
KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_SHIFT_LEFT,0,0);
shiftPressEvent.dispatch(portal);

Related

Soft keyboard on physical device doesn't show when a textbox is clicked

I'm currently using Oppo F7 to test my app that I made using Android Studio. However, when I tested it and I clicked on a textbox (or EditText), the keyboard doesn't show/pop-up. Helpppp..
Note:
- My Oppo F7 is running on Android 8.1.0 (API 27)
- It works on the emulator on my laptop but it's far to laggy for me to test
use like this
public static void showSoftKeyboard(final Context context, final EditText editText) {
try {
editText.requestFocus();
editText.postDelayed(
new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(editText, 0);
}
}
, 200);
} catch (NullPointerException npe) {
npe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
in your activity call on text click
showSoftKeyboard(this, yourEditTextToFocus);

How do I programmatically dismiss a crash dialog?

I'm building a DPC (Device Policy Controller), and one of the issues I'm seeing is that while the Play Store and Play Services are being updated, the Google Contact Sync service crashes -- leaving the typical crash dialog on the screen. Since part of the idea of the initial set up process is to have as little user interaction as possible, how can I dismiss this dialog programmatically (since I seem to be pretty much guaranteed that this will happen)?
I've tried dismissing system dialogs...
ctx.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
... but that doesn't seem to do the trick.
Since this is a DPC, anything that requires device ownership/administration is fine.
edit: Usually I have no UI on screen at the time, so if one is necessary please do mention it. Also, preferably the solution should work on at least 6.0+, if not 4.0+.
Try to do it onWindowsFocusChanged method like this for example :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!hasFocus) {
Intent ctx= new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(ctx);
}
}
I'm not sure about app crash Dialog but maybe it'll help you
AppErrorDialog can be dismissed by broadcasting ACTION_CLOSE_SYSTEM_DIALOGS if Android version is N.
ctx.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
However, AppErrorDialog won't be displayed if phone is locked.
public boolean canShowErrorDialogs() {
return mShowDialogs && !mSleeping && !mShuttingDown
&& mLockScreenShown != LOCK_SCREEN_SHOWN;
} // ActivityManagerService
Please try this code.
try {
Class ActivityManagerNative = Class.forName("android.app.ActivityManagerNative");
Class IActivityManager = Class.forName("android.app.IActivityManager");
Method getDefault = ActivityManagerNative.getMethod("getDefault", null);
Object am = IActivityManager.cast(getDefault.invoke(ActivityManagerNative, null));
Method closeSystemDialogs = am.getClass().getMethod("closeSystemDialogs", String.class);
closeSystemDialogs.invoke(am, "DPC close");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}

Anybody know how to select text in webview in android? not use emulateShiftHeld,since the docs said " This method is deprecated"

I try to select text in webview, after I select text,I need to decide what to do next.not just copy the selected text.I have more choice for user to choose from.I mean, after user selected text in webview,could I prompt out a some button for further? I tried use emulateshiftheld to solve my problem.but google's docs said " This method is deprecated".
also,I can't prompt out some choice button.
you can clck the link to see what I mean. link: https://public.blu.livefilestore.com/y1pHhMR2iyIbQN7uJ2C-9CLLJBosWAZw2r4MvD0qyyTG-QWUM-06i6HFu4Fn4oaWnHMbDyTBOa-CPwN6PwoZNifSQ/select.jpg?download&psid=1
Pasting relevant code from WebView emulateShiftHeld() on Android Newer SDK's
/**
* Select Text in the webview and automatically sends the selected text to the clipboard
*/
public void selectAndCopyText() {
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);
}
}
The question is very old, but for other people here is a solution with javascript for API 19+. Just add this to your WebView:
public void getSelectedText(final SelectedTextInterface selectedTextInterface) {
evaluateJavascript("(function(){return window.getSelection().toString()})()", new ValueCallback<String>() {
#Override
public void onReceiveValue(final String selectedText) {
if (selectedText != null && !selectedText.equals("")) {
//If you don't have a context, just call
//selectedTextInterface.onTextSelected(selectedText);
if (context instanceof Activity) {
((Activity) context).runOnUiThread(new Runnable() {
#Override
public void run() {
selectedTextInterface.onTextSelected(selectedText);
}
});
} else selectedTextInterface.onTextSelected(selectedText);
}
}
});
}
public interface SelectedTextInterface {
void onTextSelected(String selectedText);
}
When you are done with selecting text, just call it with:
webView.getSelectedText(new YourWebView.SelectedTextInterface() {
#Override
public void onTextSelected(String selectedText) {
//Your code here
}
});
I hope this may be useful for some people :-)

How to select text from a webview and not copy it?

I am creating an application which requires a webview.
I want to enable text selection in webview. (I found a few solutions but none of them worked)
Once the webview is selected, it should not be directly copied. I should be able to expand the selection range with handles (It is possible in version 2.3 and later. But I want this on versions lower than 2.3. HTC's browser gives us this option)
Any idea?
recently i solved that
put this function on create or on load in activity
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);
}
}
it will done...

How to determine the current IME in Android?

I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else).
How can I check which input method they currently have selected?
You can get a default IME, use:
Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
InputMethodManager has getEnabledInputMethodList(). You get an InputMethodManager from getSystemService() in your Activity.
Here's a bit of code I used to determine if GoogleKeyboard, Samsung Keyboard, or Swype Keyboard is used. The value returned by reflection for mCurId indicates the IME ID.
Test with the different keyboards/input methods you are looking for to find the relevant one
public boolean usingSamsungKeyboard(Context context){
return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
}
public boolean usingSwypeKeyboard(Context context){
return usingKeyboard(context, "com.nuance.swype.input/.IME");
}
public boolean usingGoogleKeyboard(Context context){
return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
}
public boolean usingKeyboard(Context context, String keyboardId)
{
final InputMethodManager richImm =
(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isKeyboard = false;
final Field field;
try
{
field = richImm.getClass().getDeclaredField("mCurId");
field.setAccessible(true);
Object value = field.get(richImm);
isKeyboard = value.equals(keyboardId);
}
catch (IllegalAccessException e)
{
}
catch (NoSuchFieldException e)
{
}
return isKeyboard;
}

Categories

Resources