Android: Backspace in WebView/BaseInputConnection
Ok, finally figured this out.
In Android 4.2 (maybe in earlier versions as well) the backspace is not sent as a sendKeyEvent(..., KeyEvent.KEYCODE_DEL)
by the standard soft keyboard. Instead, it is sent as deleteSurroundingText(1, 0)
.
So the solution in my case is to make a custom InputConnection
with the following:
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
Note: Please let me know if I am doing something stupid here, as it is my 3rd day writing for Android.
Can't get backspace to work in codemirror, under Phonegap on Android 4.x?
Override init Activity method :
public class ProjectName extends DroidGap
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
init(); // Don't forget this, you'll get runtime error otherwise!
// The following does the trick:
super.appView.getSettings().setUseWideViewPort(true);
super.appView.getSettings().setLoadWithOverviewMode(true);
// Set by <content src="index.html" /> in config.xml
super.loadUrl(Config.getStartUrl());
//super.loadUrl("file:///android_asset/www/index.html")
super.setIntegerProperty("loadUrlTimeoutValue", 10000);
}
/**
* Create and initialize web container with default web view objects.
*/
@Override
public void init() {
CordovaWebView webView = new CustomWebView(ProjectName.this);
CordovaWebViewClient webViewClient;
if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
webViewClient = new CordovaWebViewClient(this, webView);
}
else
{
webViewClient = new IceCreamCordovaWebViewClient(this, webView);
}
this.init(webView, webViewClient, new CordovaChromeClient(this, webView));
}
}
Create on CustomWebView which extends CordovaWebView
public class CustomWebView extends CordovaWebView{
public CustomWebView(Context context) {
super(context);
}
@Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
MyCustomInputConnection connection = new MyCustomInputConnection(this, false);
return connection;
}
}
Create your custom InputConnection :
public class MyCustomInputConnection extends BaseInputConnection{
public MyCustomInputConnection(View targetView, boolean fullEditor) {
super(targetView, fullEditor);
}
@Override
public boolean deleteSurroundingText(int beforeLength, int afterLength) {
// magic: in latest Android, deleteSurroundingText(1, 0) will be called for backspace
if (beforeLength == 1 && afterLength == 0) {
// backspace
return super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
&& super.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
}
return super.deleteSurroundingText(beforeLength, afterLength);
}
}
Related Topics
Android Appcompat-V7:21.0.0 Change Material Checkbox Colors
Android - Multi-Line Linear Layout
How to Change Separator Color in Navigationview
What Is the Equivalent of Actionbar in Earlier Sdk Versions
How to Send a Simple Email Programmatically? (Exists a Simple Way to Do It)
Gridview with Different Cells Sizes, Pinterest Style
Remove Bottomnavigationview Labels
Disable the Touch Events for All the Views
How to Change Background Color Popup Menu Android
How to Finish an Android Application
Detect If an App Is Installed from Play Store
How to Change the Color of the Tabs Indicator Text in Android
Do Geofences Remain Active in Android After a Device Reboot
Android Exporting to CSV and Sending as Email Attachment
Receiver Not Registered Exception Error
Android Viewpager Get the Current View