How to use HTML files from the Assets folder in a Webview

Asked

Viewed 581 times

2

I know there are several similar questions, but in all of them I have found no method to help me.

As it is in Activity:

package com.example.guilherme.webviewteste;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    webTeste();
}



public void webTeste(){

    WebView webView = (WebView) findViewById(R.id.webview1);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    String path="file:///assets/mathscribe/";
    String js = "<html><head>" + "<link rel='stylesheet' href='"+path+"jqmath-0.4.3.css'>"+"<script src='"
            +path+"jquery-1.4.3.min.js'></script>"+"<script src='"+path+"jqmath-etc-0.4.5.min.js'></script>"+
            "</head><body>"+"<script> var s = '$ax^2+bx+c=0$ with $a=0$' M.parseMath(s);document.write(s);</script></body></html>";
    webView.loadData(js, "text/html", "UTF-8");

}
}

As it is in XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.guilherme.webviewteste.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:layout_marginTop="0dp"/>
<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webview1"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp">
</WebView>

Print of the project organization: inserir a descrição da imagem aqui

Print how the final result is in Smartphone:

inserir a descrição da imagem aqui

Should be loaded the expression that is on this line

<script> 
     var s = '$ax^2+bx+c=0$ with $a=0$' M.parseMath(s);document.write(s); 
</script>
  • 1

    Just in case, put some html inside your body and tell me if the screen is still blank

  • @Guilhermenascimento I did a test and it worked, what could be the problem then? Now I got much more lost :/

  • Guilhermehs I think something like this has happened to me, when compiling and running somehow does not update the app in the emulator

  • Try to use the loadDataWithBaseURL pass the path as baseUrl and their <script src='"+path+"... just put <script src='./seuarquivo.js'>. If this does not work you can also try to create an index.html in this directory and use the loadUrl() using file:///assets/mathscribe/index.html as url

1 answer

0

You’re not closing your HTML. Insert after your </body> the </html>.

  • It did not work the same way, I will edit and tidy here, because I edited it here already, but the problem persists.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.