0

My MainActivity.java file looks like below:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String myUrl = "file:///android_asset/index.html";
        WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setSaveFormData(true);
        view.setWebChromeClient(new WebChromeClient());
        view.loadUrl(myUrl);
    }
}

Now, activity_main.xml file contains following codes:

<WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />
</android.support.constraint.ConstraintLayout>

And, in the html page index.html, I have been linking CSS and JS files like below:

<link rel="stylesheet" href="file:///android_asset/css/bootstrap.min.css">
<script src="file:///android_asset/js/popper.min.js"></script>

I am seeing the html content; but, the CSS and JavaScript files are not in action at all. The page looks plain. I am new to Android, I am certainly missing something crucial here.

2

1 Answer 1

2

In the html page index.html, use the path names relative to your html page (not file:///android_asset).

So change this:

<link rel="stylesheet" href="file:///android_asset/css/bootstrap.min.css">
<script src="file:///android_asset/js/popper.min.js"></script>

To this:

<link type="text/css" rel="stylesheet" href="css/bootstrap.min.css"/>
<script type="text/javascript" src="js/popper.min.js"></script>

with directory structure:

+---assets
¦   +---css
¦   +---js
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.