Search code examples
javascriptandroidcsswebviewandroid-assets

Load CSS and JS files from assets folder in android webview


I want to load CSS and JS files localy from android assets folder for better perfomance in android webview,Is it possible? I appreciate your help,thanks


Solution

  • In android project

    WebView myweb = FindViewById<WebView>(Resource.Id.webView1);
    myweb.LoadUrl("file:///android_asset/index.html");
    

    In index.html

    <link href="style.css" rel="stylesheet" type="text/css">
    <script src="script.js"></script> 
    

    And if you need to increase android webview performance and graphic change this properties

    myweb.Settings.JavaScriptEnabled = true;
    myweb.Settings.DisplayZoomControls = false;
    myweb.Settings.SetRenderPriority(WebSettings.RenderPriority.High);
    if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
    {
       myweb.SetLayerType(Android.Views.LayerType.Hardware, null);
    }
    

    if you are using android studio : view this link

    if you need only js or css files view this Answers: