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()");
        }

    });

}
}
David
  • 11
  • 1
  • 2

1 Answers1

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.

Community
  • 1
  • 1
Kunal Khaire
  • 307
  • 2
  • 7
  • 2
    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 https://my.gediz.edu.tr/ answer here for others -> http://stackoverflow.com/questions/10187908/fill-form-in-webview-with-javascript%3E and http://stackoverflow.com/questions/7961568/fill-fields-in-webview-automatically – David Dec 29 '15 at 20:42
  • Glad that you found the answer – Kunal Khaire Dec 30 '15 at 13:05