I'm replicating an app from iOS in Android
Here's how to fill in a form and click a button in a web view:
[gist 8b015912875c8f1e693e /]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Button button = (Button) findViewById(R.id.button); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar); | |
final WebView webView = (WebView) findViewById(R.id.webView); | |
//Set links to show in webview not open in another program. | |
webView.setWebViewClient(new WebViewClient()); | |
webView.getSettings().setJavaScriptEnabled(true); | |
String url = "http://www.alexhedley.com/form.asp"; | |
webView.loadUrl(url); | |
View.OnClickListener listener = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
String barcode = "barcode"; | |
String fillIn = "document.getElementById('barcode').value = '" + barcode + "';"; | |
String click = "(function(){document.getElementById('btnEnter').click();})()"; | |
String combined = "javascript:" + fillIn + click; | |
webView.loadUrl(combined); | |
} | |
}; | |
button.setOnClickListener(listener); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v7.widget.Toolbar | |
android:id="@+id/myToolbar" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form method="POST" action="form-process.asp"> | |
<label for="barcode">Barcode: </label> | |
<input id="barcode" name="barcode" type="text" value=""> | |
<br /> | |
<input id="btnEnter" name="btnEnter" type="submit" value="Submit"> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<uses-permission | |
android:name="android.permission.INTERNET"> | |
</uses-permission> |
activity.java
Button button = (Button) findViewById(R.id.button);
Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
final WebView webView = (WebView) findViewById(R.id.webView);
//Set links to show in webview not open in another program.
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
String url = "http://www.alexhedley.com/form.asp";
webView.loadUrl(url);
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String barcode = "barcode";
String fillIn = "document.getElementById('barcode').value = '" + barcode + "';";
String click = "(function(){document.getElementById('btnEnter').click();})()";
String combined = "javascript:" + fillIn + click;
webView.loadUrl(combined);
}
};
button.setOnClickListener(listener);
activity.xml
<android.support.v7.widget.Toolbar
android:id="@+id/myToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
form.html
<form method="POST" action="form-process.asp">
<label for="barcode">Barcode: </label>
<input id="barcode" name="barcode" type="text" value="" />
<br />
<input id="btnEnter" name="btnEnter" type="submit" value="Submit" />
</form>
manifest.xml
<uses-permission
android:name="android.permission.INTERNET">
</uses-permission>