There is no really pretty and short way in doing so.
The problem is that everytime you load a new page you run the code for logging in on every single page.
To work around that problem you should save wether you already logged into the application, simplest approach is as follows:
wblogin.setWebViewClient(new WebViewClient() {
private boolean shallLogin = true;
@Override
public void onPageFinished(WebView view, String url) {
if (shallLogin == true) {
wblogin.loadUrl("javascript: {" +
"document.getElementById('email').value = '" + user_name + "';" +
"document.getElementById('password').value = '" + pass_name + "';" +
"document.getElementById('xyz').click();" +
"};");
shallLogin = false;
}
}
});
As the login might fail or the user might move around without being logged in a more proper way would be to register a JS Callback and on page reload check if the login were successful by finding an element by ID which is only visible when logged in like the userimage or something.
...
@Override
public void onPageFinished(WebView view, String url) {
wblogin.loadUrl("javascript: {" +
"window.ANDROID_CALLBACK.login(document.getElementsByClassName('.my-user-img').length > 0);"
"};");
}
...
class LoadListener {
public LoadListener (MyWebViewClient myWebViewClient) {
}
public void login(boolean loginWorked) {
wblogin.shallLogin = !loginWorked;
}
}
...
wblogin.addJavascriptInterface(new LoadListener(wblogin), "ANDROID_CALLBACK")