Easy
Easy

Reputation: 159

Display WebPage without using mobile connection

I am using the WebView of Android so that I will not have to program the zoom and scroll. I have the web page that I want in the assets folder and here is my code. Each time I run in the emulator I am getting the error, no such file or directory. My question is what do I need to change in order for this to work?

Just updated my .java to this and now I receive a force close when I press button1 and my XML is the same as below.

OK I fixed that last part and now all that is happening is I get a blank screen once the app is opened.

package com.DS;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;

public class CheatMathActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

            WebView webView = (WebView) findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.getSettings().setSupportZoom(true);
            webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

    } });  
} 

}    

Ok Here is my XML, before the added WebView I had somewhat of a home activity that had two buttons and a textview, once I add the WebView all that the graphical layout shows is a grey screen that says WebView in the center.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#4876ff" >
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginTop="90dp"
    android:textColor="#0000ff"
    android:text="AP Calculus" />
 <Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:layout_marginTop="38dp"
    android:text="More Coming Soon!" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="87dp"
    android:textColor="#FFFAFA"
    android:text="If you have any questions or want to report a bug, feel free to email                        me at [email protected]
                    Thank you!" />
<TextView
    android:id="@+id/titleView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:gravity="center_vertical"
    android:text="Welcome to Math Cheat Sheet!"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
</RelativeLayout>

Upvotes: 0

Views: 410

Answers (2)

Nesim Razon
Nesim Razon

Reputation: 9794

Put a webview on your layout. and on your activity load your html asset like this.

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

And don't forget to put file stuffyoumustknowcoldforapcalc.htm in folder named assets.

package com.DS;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.webkit.WebView;

public class CheatMathActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    final Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){

                webView = (WebView) findViewById(R.id.webView1);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.getSettings().setBuiltInZoomControls(true);
                webView.getSettings().setSupportZoom(true);
                webView.loadUrl("file:///android_asset/stuffyoumustknowcoldforapcalc.htm");

        } });  
    } 


}  

Upvotes: 1

a.bertucci
a.bertucci

Reputation: 12142

Well, if you use an Intent (as you posted above) a new external browser instance shows up and loads the Uri set in the Intent instead of using your own WebView. In order to achieve what you're asking you have to get a reference to the WebView inside your activity layout and then load your page. Furthermore, if you want to load your page from your assets/ folder you should use AssetManager instead, and the WebView.loadData() method. Your onCreate() method should look like this:

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  // get a reference to the webview
  final WebView wV = (WebView) findViewById(R.id.webview);

  // get page content from assets/your_page.html as a String
  InputStream is = getAssets().open("your_page.html");
  BufferedReader br = new BufferedReader(new InputStreamReader(is));
  StringBuffer sb = new StringBuffer();
  String line;
  while((line=br.readLine())!=null){
    sb.append(line);
    sb.append("\n");
  }
  is.close();
  final String html = sb.toString();

  // get a reference to the button
  Button button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v){

      // load html string into your WebView
      wV.loadData( html, "text/html", "utf-8" );
    }
  });

}

Upvotes: 2

Related Questions