Reputation: 1101
I have an android app that's using webview. Several forms I'm using have autofocus on the first element. What I want is for the relevant keyboard to display when the form is loaded. The code in the activity is:
public class MyActivity extends DroidGap {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//load the main app
loadUrl("file:///android_asset/www/index.html");
}
@Override
public void init() {
final String version = getVersion();
CordovaWebView webView = new CordovaWebView(this);
init(webView, new CordovaWebViewClient(this, webView) {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
super.onPageFinished(view, url);
boolean redirected = false;
//If the page you're accessing is the login page
if (url.endsWith("/Login")) {
//redirect to root page with query string
appView.loadUrl(url + "?version=" + version));
redirected = true;
}
return redirected;
}
}, new CordovaChromeClient(this, webView));
}
}
Basically what is happening in the code above the index page of the app is loaded. When I click on the login button the app version is appended to the url that is loaded (for reasons unrelated) and the login page is displayed. When the page loads I can see the cursor inside the field but the keyboard won't display until I have pressed on the field.
I have looked at this question but couldn't figure out how to apply the answers. Any help is greatly appreciated thank you.
Upvotes: 1
Views: 2414
Reputation: 1101
I ended up using
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// Display the keyboard automatically when relevant
if (view.getTitle().equals("Login")) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, 0);
}
}
Upvotes: 1