Load HTML file into WebView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Loading an HTML file into an Android WebView is simple once you know where the file lives and how WebView resolves paths. The two most common cases are loading a bundled file from the app's assets directory and loading HTML text directly from a string when the content is generated at runtime.
Load a Bundled HTML File from assets
If the HTML file is part of your app package, place it in app/src/main/assets/ and load it with a file:///android_asset/ URL.
activity_main.xml:
MainActivity.kt:
That is the cleanest solution when the page is static documentation, offline help, or bundled local content.
Enable Only the Settings You Actually Need
A plain local HTML page often works with the default settings, but some pages need JavaScript, local storage, or custom navigation handling. Enable those deliberately rather than copying a long settings block from the internet.
Enable JavaScript only if the local page truly needs it. A static FAQ page usually does not.
Load HTML from a String with loadDataWithBaseURL
If your HTML is generated at runtime, use loadDataWithBaseURL instead of loadUrl.
This method is also useful when you want the content to reference local CSS or images. In that case, pass a base URL that points to the assets location.
That tells the WebView how to resolve relative paths inside the HTML string.
Keep Navigation Inside the App
By default, some links may open in the system browser instead of staying inside your WebView. If you want local navigation to remain embedded, assign a WebViewClient.
This small line is often the difference between an in-app help page and a jump out to Chrome.
Organize Local Resources Correctly
If your HTML file uses images, CSS, or JavaScript, place those files in assets as well and reference them relative to the HTML file.
Example layout:
- '
assets/help.html' - '
assets/styles.css' - '
assets/logo.png' - '
assets/app.js'
Inside help.html:
That keeps the local page self-contained and offline-friendly.
Common Pitfalls
The biggest mistake is putting the HTML file in res/raw and then trying to load it with file:///android_asset/, which only works for the assets directory. Another common issue is using loadData() for content that depends on relative paths, which breaks linked CSS and images because there is no base URL. Developers also often enable JavaScript by default even when the page does not need it, which expands the attack surface for no benefit. Finally, without a WebViewClient, links may open outside the app and make the page feel broken.
Summary
- Put bundled HTML files in
app/src/main/assets/and load them withfile:///android_asset/.... - Use
loadDataWithBaseURL()when the HTML is generated at runtime or needs relative local resources. - Enable
WebViewsettings such as JavaScript only when required. - Set a
WebViewClientif you want navigation to stay inside the app. - Keep CSS, images, and scripts in
assetstoo so the page works offline and consistently.
Related reading
- Load javascript async, then check DOM loaded before executing callback
- Load jQuery Mobile script asynchronously?
- Load local html into UIWebView using swift
- Load resources from relative path using local html in uiwebview
- Load view from an external xib file in storyboard
- Loaded nib but the 'view' outlet was not set
- Load Tensorflow js model from local file system in javascript
- Load uservoice API on Click
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.