1

I am creating an app to help me and other students log into our school's website quickly. The school website is https://my.gediz.edu.tr/

I created an application with a WebView the thing is I want that the webview enters username and password automatically so that the user is already logged in when he starts the application. I googled it and found something like that but it does not work.

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView txtMsg;
WebView webview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    android.support.v7.app.ActionBar actionBar=getSupportActionBar();

    // ActionBar actionBar=getActionBar();
    actionBar.setLogo(R.drawable.ic_actionbar_gediz1);
    actionBar.setDisplayUseLogoEnabled(true);
    actionBar.setDisplayShowHomeEnabled(true);

    txtMsg=(TextView)findViewById(R.id.textView1);
    //webview=(WebView)findViewById(R.id.webview1);
    demo1TrySpecificURL();

    //zoom
    webview.getSettings().setBuiltInZoomControls(true);
}

private void demo1TrySpecificURL(){
    webview=(WebView)findViewById(R.id.webview1);
    webview.getSettings().setJavaScriptEnabled(true);
    //webview.setWebViewClient(new MyWebClient(txtMsg, "https://my.gediz.edu.tr/"));
    webview.loadUrl("https://my.gediz.edu.tr/");

    webview.setWebViewClient(new WebViewClient(){

        public void onPageFinished(WebView view, String url){



            view.loadUrl("javascript:document.getElementsByName('username')[0].value = 'username'");
            view.loadUrl("javascript:document.getElementsByName('password')[0].value = 'password'");


            view.loadUrl("javascript:document.forms['Login'].submit()");
        }

    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    //return super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu_main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.forward_page) {
        webview.goForward();
        return true;
    }

    else if (id == R.id.back_page) {
        webview.goBack();
        return true;
    }

    else if (id == R.id.reLoad_page) {
        webview.reload();
        return true;
    }

    else if (id == R.id.zoom_in) {
        webview.zoomIn();
         return true;
    }

    else if (id == R.id.zoom_out) {
        webview.zoomOut();
        return true;
    }



    return super.onOptionsItemSelected(item);
}

public void blaHandler(View view) {

    webview.setWebViewClient(new WebViewClient(){

        public void onPageFinished(WebView view, String url){

            view.loadUrl("javascript:document.getElementsByName('username')[0].value = 'username'");
            view.loadUrl("javascript:document.getElementsByName('password')[0].value = 'userpassword'");

            view.loadUrl("javascript:document.forms['Login '].submit()");
        }

    });

}
}

1 Answer 1

1

Try something as suggested here,

Android: insert data into html input

Basically,you have to write javascript handlers and hook it onto your webview.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you I found the answer is works for other websites but NOT MINE because when i inspect the elements there is No id for text fields my.gediz.edu.tr answer here for others -> stackoverflow.com/questions/10187908/… and stackoverflow.com/questions/7961568/…
Glad that you found the answer

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.