Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load android assets css files to android WebView URL

Actually I want to load complete site from internet link but i want to load css files from mobile (means not from internet). Because I want to load all page faster using internal local css file.

Example :

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="dist/css/font-awesome.min.css" />
    </head>
</html>

So Here I want to Load "bootstrap.min.css" and "font-awesome.min.css" from mobile memory only.

My Android Code :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String url = "http://www.MySiteName.com/";
    view = (WebView) this.findViewById(R.id.webView);

    view.getSettings().setJavaScriptEnabled(true); // Enable JavaScript Support
    view.setWebViewClient(new WebViewClient()); // Links open or Navigate in same webView not in Browser

    view.loadUrl(url);
}
like image 369
Irshad Khan Avatar asked Jul 20 '15 12:07

Irshad Khan


People also ask

How do I display HTML content in WebView?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken web view to show html content.

How do I enable JavaScript on Android WebView?

Enable JavaScript JavaScript is disabled in a WebView by default. You can enable it through the WebSettings attached to your WebView .You can retrieve WebSettings with getSettings() , then enable JavaScript with setJavaScriptEnabled() . WebView myWebView = (WebView) findViewById(R.id.webview);

How do I import WebView?

Modify src/MainActivity. java file to add WebView code. Run the application and choose a running android device and install the application on it and verify the results. Following is the content of the modified main activity file src/MainActivity.

Is Android WebView deprecated?

The Android system webview custom cache file has been deprecated and removed in Android 13. New apps and any app updates will now use the operating system default cache location.


2 Answers

Please put CSS in assets folder, and refer to CSS by relative path, and load HTML to WebView by loadDataWithBaseURL() method: if your css file name is mycss.css

StringBuilder data = new StringBuilder();
data .append("<HTML><HEAD><LINK href=\"mycss.css\" type=\"text/css\" rel=\"stylesheet\"/></HEAD><body>");
data .append(tables.toString());
data .append("</body></HTML>");
webView.loadDataWithBaseURL("file:///android_asset/", data .toString(), "text/html", "utf-8", null);

thank you

like image 134
Jay Dwivedi Avatar answered Sep 18 '22 08:09

Jay Dwivedi


Because the two other answers are just copy pastas and I happened to have the same issue I will help you with my solution:

You need to specify the file path in the head section of your html file. Pay attention to the part after href

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="stylesheet" href="file:///android_asset/bootstrap.min.css" />
        <link rel="stylesheet" href="file:///android_asset/font-awesome.min.css" />
    </head>
</html>

I just can't get my head around why the other posters just copied and pasted and didn't even read what you were trying to tell them.

I hope this helps.

Be aware that your resources folder can be named differently.

like image 29
Horsty Avatar answered Sep 20 '22 08:09

Horsty