Sunday, 8 June 2014

Android accessing html control inside Java class| Getting html field value inside android activity class

Hello Friends,
                This is my another android tutorial. In this android demo I am going to
cover following parts:

1. Showing Android Login form in web view or loading html file in web view.
2. Getting the control  of log in button click,user name and password value inside
    our java class.
3. Checking the field validation inside  our java code.
4. Calling web service method based on valid user name and password.





Source Code:

1. Login.html :  This is my html file in which there is two edittext and a login button.
                           When I m clicking on login Button  I am checking the value of both
this text field inside my java class and if it is valid   then I am calling my login service
url.


<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Login Form</title>
<link rel="stylesheet" href="style.css">
<script type="text/javascript">
  function showAndroidToast() {
   var name = document.getElementById("loginName").value;
   var password = document.getElementById("password").value;
   Android.showMyToast(name,password);
  }
   </script>

</head>
<body>
 <section class="container">
  <div class="login">
   <h1>Login to Android Developer Solutions</h1>
   <form>
    <p>
     <input type="text" name="login" id="loginName" value=""
      placeholder="Username or Email">
    </p>
    <p>
     <input type="password" name="password" id="password" value=""
      placeholder="Password">
    </p>
    <p class="remember_me">
     <label> <input type="checkbox" name="remember_me"
      id="remember_me"> Remember me
     </label>
    </p>
    <p class="submit">
     <input type="submit" name="commit" value="Login"
      onClick="showAndroidToast()">
    </p>
   </form>
  </div>

  <div class="login-help">
   <p>
    Forgot your password? <a href="index.html">Click here to reset
     it</a>.
   </p>
  </div>
 </section>

 <section class="about">
  <p class="about-author">
   &copy; 2013&ndash;2014 <a
    href="http://www.androiddevelopersolutions.com/" target="_blank">Android
    Developer Solution</a><br> By <a
    href="http://www.androiddevelopersolutions.com/" target="_blank">Mukesh
    Y</a>
 </section>

</body>
</html>


2. MainActivity.java

package com.mukesh.webview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends Activity {

 @SuppressLint("SetJavaScriptEnabled")
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  WebView webView = (WebView) findViewById(R.id.webview);

  WebSettings webSettings = webView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new WebAppInterface(this), "Android");
  webView.loadUrl("file:///android_asset/index.html");
 }

}


Download Code : WebView Demo

Enjoy Codiing....
Cheers :)

Mukesh Yadav

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developer Blog.

Blog Widget by LinkWithin