There is no guarantee that there will be only one Form
in the page the user has loaded.
However, if it's for the Google
Search Form in particular, you can then execute a JavaScript
to get a Form
in the Google.com
page that has id=tsf
(which is the unique id of the search form in Google.com
).
Nevertheless, you mentioned in your comment:
This will happen to all other pages
Unless you know the id
of the form in the page (which is practically impossible for every page in WWW
that contains more than one Form
).
You can loop through all Forms
and remove the last char
of the text in the Form
's input
of type text
.
Example
First create a method to run the JS
script (note that there are different ways to execute a JS
in WebView
in Android, for more details in case the following method did not work for you, look here):
public void run(final String script) {
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.loadUrl("javascript:" + script);
}
});
}
Then create a method that returns a JS String to:
- Get all Forms in the page.
- Cycle through them all.
- Cycle through the elements of each Form..
- Set its value to (its old value - last char).
private String deleteLastCharScript() {
return "var listOfForms = document.forms;\n" +
"for(var i = 0; i < listOfForms.length; i++) {\n" +
" var elements = listOfForms[i].elements;\n" +
" for(var j = 0 ; j < elements.length ; j++){\n" +
" var item = elements.item(j);\n" +
" item.value = item.value.substring(0, item.value.length - 1);\n" +
" }\n" +
"};";
}
You can run that script in the onBackPressed()
method like this:
@Override
public void onBackPressed() {
run(deleteLastCharScript());
}
This is MCVE:
Add to Manifest:
<uses-permission android:name="android.permission.INTERNET" />
Create Simple WebView
Layout:
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Implementation in Activity:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
mWebView = findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true); // enable JS
mWebView.loadUrl("https://www.google.com");
}
/**
* Execute JS String Asynchronously
* @param script
*/
public void run(final String script) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
// run JavaScript asynchronously
// it works on KitKat onwards
mWebView.evaluateJavascript(script, null);
}else {
// use different thread to run JavaScript asynchronously
// because evaluateJavascript doesn't work on versions before KitKat
new Thread(new Runnable() {
@Override
public void run() {
mWebView.post(new Runnable() {
@Override
public void run() {
mWebView.loadUrl("javascript:" + script);
}
});
}
}).start();
}
}
/**
* This JS Script tp Loop through all
* inputs in all forms in the page
* and remove last char from each
* @return
*/
private String deleteLastCharScript() {
return "var listOfForms = document.forms;\n" +
"for(var i = 0; i < listOfForms.length; i++) {\n" +
" var elements = listOfForms[i].elements;\n" +
" for(var j = 0 ; j < elements.length ; j++){\n" +
" var item = elements.item(j);\n" +
" item.value = item.value.substring(0, item.value.length - 1);\n" +
" }\n" +
"};";
}
@Override
public void onBackPressed() {
run(deleteLastCharScript());
}
}
Result
