Pages

About

Pages - Menu

Ads 468x60px

Social Icons

Main Menu

Featured Posts

Monday, 11 August 2014

Webview load html from assets directory

I'm trying to load a html page from the assets directory. I tried this, but it fails.

 
 MAIN ACTIVITY
 package com.example.muthu;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class JsExampleMain extends Activity {
    private WebView wv;
    Button button1;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        //create interface
        JsInterface jsInterface = new JsInterface();
       
        //get webview and enable js
        wv = (WebView) findViewById(R.id.web_view);
        wv.getSettings().setJavaScriptEnabled(true);
       
        //add interface
        wv.addJavascriptInterface(jsInterface, "android");//android is the keyword that will be exposed in js
       
        //load file
        wv.loadUrl("file:///android_asset/testt.html");
           
  }
private static void setBackgroundColor(int white) {
    // TODO Auto-generated method stub
   
}
       
    }
     
      //javascript interface
    class JsInterface{
        public JsInterface() {
            // TODO Auto-generated constructor stub
        }

        //function that will be called from assets/test.js
        //js example: android.log('my message');
        public void log(String msg){
            Log.d("MSG FROM JAVASCRIPT", msg);
        }
    }


XML_MAIN

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webView2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


HTML_CODE 1

<!DOCTYPE html>
<html>
<body bgcolor="#98FB98">


<center><p>TNPSC QUIZ</p></center>

<center><p></p></center>
<center><p></p></center>

<center><input type="button" name="b1" value="TEST ONE" onclick=" location.href='texta.html'"style="height: 40px; width: 300px;background-color:BlueViolet"></center>

<center><p></p></center>
<center><p></p></center>

<center><input type="button" name="b2" value="TEST TWO" onclick="location.href='ques.html'"style="height: 40px; width:300px;background-color:BlueViolet"></center>

<center><p></p></center>
<center><p></p></center>

<center><input type="button" name="b3" value="TEST FOUR" onclick="location.href='ques.html'"style="height: 40px; width: 300px;background-color:BlueViolet"></center>

<p id="demo"></p></center>

</body>
</html>

HTML_CODE 2

<!-- TWO STEPS TO INSTALL BASIC JAVASCRIPT QUIZ:

  1.  Copy the coding into the HEAD of your HTML document
  2.  Add the last code into the BODY of your HTML document  -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document  -->

<HEAD>
<body bgcolor="#98FB98">
<style type="text/css">
<!--
.bgclr {background-color: white; color: black; font-weight: bold;}
-->
</style>

<script language="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://www.javascriptsource.com -->

<!-- Begin
// Insert number of questions
var numQues = 6;

// Insert number of choices in each question
var numChoi = 3;

// Insert number of questions displayed in answer area
var answers = new Array(6);

// Insert answers to questions
answers[0] = "1979";
answers[1] = "Reinforced glass";
answers[2] = "Netscape";
answers[3] = "Common Gateway Interface";
answers[4] = "Mercury";
answers[5] = "Bahadur Shah";

// Do not change anything below here ...
function getScore(form) {
  var score = 0;
  var currElt;
  var currSelection;
  for (i=0; i<numQues; i++) {
    currElt = i*numChoi;
    for (j=0; j<numChoi; j++) {
      currSelection = form.elements[currElt + j];
      if (currSelection.checked) {
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
      }
    }
  }
  score = Math.round(score/numQues*100);
  form.percentage.value = score + "%";
  var correctAnswers = "";
  for (i=1; i<=numQues; i++) {
    correctAnswers += i + ". " + answers[i-1] + "\r\n";
  }
  form.solutions.value = correctAnswers;
}
//  End -->
</script>

</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<h3>TNPSC Quiz</h3>
<p><font size="1">
<form name="quiz">
1. When did Mother Teresa win the Nobel Peace Prize?
<ul style="margin-top: 1pt">
  <input type="radio" name="q1" value="1975">1975<br>
  <input type="radio" name="q1" value="1979 ">1979 <br>
  <input type="radio" name="q1" value="1981">1981<br>
</ul>

2. Which one of the following glasses is used in bullet proof screens?
<ul style="margin-top: 1pt">
  <input type="radio" name="q2" value="Soda glass">Soda glass<br>
  <input type="radio" name="q2" value="Reinforced glass">Reinforced glass<br>
  <input type="radio" name="q2" value="Dynamic HTML">Dynamic HTML<br>
</ul>
3. Which substance is used to retard the setting action of cement?
<ul style="margin-top: 1pt">
  <input type="radio" name="q3" value="Microsoft">Microsoft<br>
  <input type="radio" name="q3" value="Netscape">Netscape<br>
  <input type="radio" name="q3" value="Sun Micro Systems">Sun Micro Systems<br>
</ul>
4. What does CGI stand for?
<ul style="margin-top: 1pt">
  <input type="radio" name="q4" value="Cascading Gate Interaction">Cascading Gate Interaction<br>
  <input type="radio" name="q4" value="Common GIF Interface">Common GIF Interface<br>
  <input type="radio" name="q4" value="Common Gateway Interface">Common Gateway Interface<br>
</ul>
5. The mineral in which India depends largely on imports is?
<ul style="margin-top: 1pt">
  <input type="radio" name="q5" value="Mercury">Mercury<br>
  <input type="radio" name="q5" value="Mica">Mica<br>
  <input type="radio" name="q5" value="Iron Ore">Iron Ore<br>
</ul>
6. The last Mughal Emperor was?
<ul style="margin-top: 1pt">
  <input type="radio" name="q6" value="Babur">Babur<br>
  <input type="radio" name="q6" value="NoorJehan">NoorJehan<br>
  <input type="radio" name="q6" value="Bahadur Shah">Bahadur Shah<br>
</ul>

<input type="button" value="Get score" onClick="getScore(this.form)">
<input type="reset" value="Clear answers">
<p> Score = <strong><input class="bgclr" type="text" size="6" name="percentage" disabled></strong><br><br>
</font></p>
Correct answers:<br>
<textarea class="bgclr" name="solutions" wrap="virtual" rows="6" cols="30" disabled>
</textarea>

<p>Click the button to trigger a function.</p>

<input type="button" name="b3" value="Back" onclick="location.href='buuton.html'"

<p id="demo"></p>



</form>

<!-- Script Size:  3.82 KB -->

screenshot




0 comments:

Post a Comment

Followers