1

In my app I am having webview to load web pages Now I want to automatically login to the website loaded in the webview eg:Facebook.Help me to solve this.I am using Android Studio to develop app. This is my code.

public class MainActivity extends AppCompatActivity {
EditText url;
Button go;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webView=(WebView)findViewById(R.id.wv);
    url=(EditText)findViewById(R.id.urlEdit);
    go=(Button)findViewById(R.id.goBut);
    webview.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    go.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            theWebsite=url.getText().toString();
            webView.loadUrl(theWebsite);
        }
    });
6
  • please post your code Commented Feb 26, 2018 at 11:01
  • you need to develop web service at server side and from webservice you can redirect directly login page in webview Commented Feb 26, 2018 at 11:09
  • Bro I want to enter username and password programmatically in webview page Commented Feb 26, 2018 at 11:16
  • So do you want to populate username and password and submit it automatically (without any user intervention)? Commented Feb 26, 2018 at 11:42
  • Yes Bro thats it Commented Feb 26, 2018 at 11:52

2 Answers 2

2

step 1) first of all enable javascript in your webview

step 2) on onPageFinished using javascript autofill username and password field and generate click on login button

wv.loadUrl("javascript:document.getElementsByName('Username').value = 'abc'");
wv.loadUrl("javascript:document.getElementsByName('Pass').value = 'xyz'");
wv.loadUrl("javascript:document.forms['Login'].submit()");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks bro for your answer
This did not work for me. It showed a blank while screen with my username. my username should actually be seen in the username field
Try to use document.getElementById
@DineshS , even after using document.getElementById , i am facing same issue just like @abhishekmaharajpet
in javascript:document.forms['Login'].submit()") what is 'Login' for Abhinay Sharma
0

This explanation helped me. Fill form in WebView with Javascript

view.evaluateJavascript(js, new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String s) {
                }

instead of

view.loadUrl(js);

which is the answer I found everywhere else.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.