안드로이드 개발 Android WebView 로컬 HTML 파일 표현하는 방법
안드로이드에서 웹 페이지를 불러오고 싶다면 WebView 위젯을 사용해야 합니다. WebView 에 URL 을 넘겨서 해당 사이트를 호출하는 것이죠. 그런데 외부에서 호출한 웹 사이트가 아닌 앱이 가지고 있는 html 파일을 어떻게 하면 WebView 로 불러 올 수 있을까요? 오늘은 앱에 있는 Local html 을 파일을 불러와서 WebView 에 표현하는 방법을 알아 보겠습니다.
▼ 보통 WebView 를 이용해서 외부 사이트를 불러오고 싶다면 다음과 같이 입력합니다. loadUrl() 함수에 사이드 주소를 넘기는 것이죠. 아래 결과 화면에 제 블로그가 나타났네요.
WebView wv = (WebView)findViewById(R.id.wvlocal); wv.loadUrl("//mainia.tistory.com");▼ 이번에는 앱 안에 html 파일을 만들어서 WebView 로 불러 오겠습니다. 보통 자바 소스가 아닌 Resources 파일들은 app/src/main/res 아래 drawable, layout, values 등의 폴더에 저장합니다. 그런데 html 파일을 포함한 그 외의 데이터들은 app/src/main/assets 에 저장하는 것이 관례입니다. html 파일은 assets/www 폴더에 저장합니다. 해당 폴더를 가리키는 소스상 URL 은 file:///android_asset/www입니다. 그러니까 실제 폴더는 assets 지만 소스상에서는 android_asset 가 되는 것이죠. 그럼 assets/www 폴더에 index.html 파일을 만들어서 화면에 표현할 소스를 입력합니다.
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=euc-kr" /> <title>제목</title> </head> <body> <p>Hello World!</p> </body> </html>▼ loadUrl() 함수에 사이트 주소 대신 file:///로 시작하는 문자열을 입력합니다. 앱 내부에 assets 폴더는 android_asset 로 접근할 수 있습니다.
▼ 그림은 위에서 만든 HTML 파일을 WebView 에 표현한 결과 입니다. 이상으로 로컬 html 파일을 WebView 에 표현하는 방법에 대해 알아 보았습니다.
- Platform
- Android Studio
- Google Play
- Jetpack
- Kotlin
-
Docs
- Overview
- Guides
- Reference
- Samples
- Design & Quality
- Games
public
class
Html
extends Object
This class processes HTML strings into displayable styled text. Not all HTML tags are supported.
Summary
interface | Html.ImageGetter Retrieves images for HTML <img> tags. |
interface | Html.TagHandler Is notified when HTML tags are encountered that the parser does not know how to interpret. |
int | FROM_HTML_MODE_COMPACT Flags for fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler): Separate block-level elements with line breaks (single newline character) in between. |
int | FROM_HTML_MODE_LEGACY Flags for fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler): Separate block-level elements with blank lines (two newline characters) in between. |
int | FROM_HTML_OPTION_USE_CSS_COLORS Flag indicating that CSS color values should be used instead of those defined in Color. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE Flag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_DIV Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_HEADING Flag indicating that texts inside <h2>~<h6> elements will be separated from other texts with one newline character by default. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_LIST Flag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM Flag indicating that texts inside <li> elements will be separated from other texts with one newline character by default. |
int | FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH Flag indicating that texts inside <p> elements will be separated from other texts with one newline character by default. |
int | TO_HTML_PARAGRAPH_LINES_CONSECUTIVE Option for toHtml(android.text.Spanned, int): Wrap consecutive lines of text delimited by '\n' inside <p> elements. |
int | TO_HTML_PARAGRAPH_LINES_INDIVIDUAL Option for toHtml(android.text.Spanned, int): Wrap each line of text delimited by '\n' inside a <p> or a <li> element. |
static String |
escapeHtml(CharSequence text)
Returns an HTML escaped representation of the given plain text. |
static Spanned |
fromHtml(String source, int flags)
Returns displayable styled text from the provided HTML string. |
static Spanned |
fromHtml(String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
This method was deprecated in API level 24. use fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler) instead. |
static Spanned |
fromHtml(String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)
Returns displayable styled text from the provided HTML string. |
static Spanned |
fromHtml(String source)
This method was deprecated in API level 24. use fromHtml(java.lang.String, int) instead. |
static String |
toHtml(Spanned text, int option)
Returns an HTML representation of the provided Spanned text. |
static String |
toHtml(Spanned text)
This method was deprecated in API level 24. use toHtml(android.text.Spanned, int) instead. |
From class java.lang.Object
|
Constants
FROM_HTML_MODE_COMPACT
Added in API level 24
public static final int FROM_HTML_MODE_COMPACTFlags for fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler): Separate block-level elements with line breaks (single newline character) in between. This inverts the Spanned to HTML string conversion done with the option TO_HTML_PARAGRAPH_LINES_INDIVIDUAL.
Constant Value: 63 (0x0000003f)
FROM_HTML_MODE_LEGACY
Added in API level 24
public static final int FROM_HTML_MODE_LEGACYFlags for fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler): Separate block-level elements with blank lines (two newline characters) in between. This is the legacy behavior prior to N.
Constant Value: 0 (0x00000000)
FROM_HTML_OPTION_USE_CSS_COLORS
Added in API level 24
public static final int FROM_HTML_OPTION_USE_CSS_COLORSFlag indicating that CSS color values should be used instead of those defined in Color.
Constant Value: 256 (0x00000100)
FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE
Added in API level 24
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTEFlag indicating that texts inside <blockquote> elements will be separated from other texts with one newline character by default.
Constant Value: 32 (0x00000020)
FROM_HTML_SEPARATOR_LINE_BREAK_DIV
Added in API level 24
Flag indicating that texts inside <div> elements will be separated from other texts with one newline character by default.
Constant Value: 16 (0x00000010)
FROM_HTML_SEPARATOR_LINE_BREAK_HEADING
Added in API level 24
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADINGFlag indicating that texts inside <h2>~<h6> elements will be separated from other texts with one newline character by default.
Constant Value: 2 (0x00000002)
FROM_HTML_SEPARATOR_LINE_BREAK_LIST
Added in API level 24
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LISTFlag indicating that texts inside <ul> elements will be separated from other texts with one newline character by default.
Constant Value: 8 (0x00000008)
FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM
Added in API level 24
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEMFlag indicating that texts inside <li> elements will be separated from other texts with one newline character by default.
Constant Value: 4 (0x00000004)
FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH
Added in API level 24
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPHFlag indicating that texts inside <p> elements will be separated from other texts with one newline character by default.
Constant Value: 1 (0x00000001)
TO_HTML_PARAGRAPH_LINES_CONSECUTIVE
Added in API level 24
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVEOption for toHtml(android.text.Spanned, int): Wrap consecutive lines of text delimited by '\n' inside <p> elements. BulletSpans are ignored.
Constant Value: 0 (0x00000000)
TO_HTML_PARAGRAPH_LINES_INDIVIDUAL
Added in API level 24
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUALOption for toHtml(android.text.Spanned, int): Wrap each line of text delimited by '\n' inside a <p> or a <li> element. This allows ParagraphStyles attached to be encoded as CSS styles within the corresponding <p> or <li> element.
Constant Value: 1 (0x00000001)
Public methods
escapeHtml
Added in API level 16
public static String escapeHtml (CharSequence text)Returns an HTML escaped representation of the given plain text.
text | CharSequence |
String |
fromHtml
Added in API level 24
public static Spanned fromHtml (String source, int flags)Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
source | String |
flags | int |
Spanned |
fromHtml
Added in API level 1
Deprecated in API level 24
This method was deprecated in API level 24.
use fromHtml(java.lang.String, int, android.text.Html.ImageGetter, android.text.Html.TagHandler) instead.
Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.
source | String |
imageGetter | Html.ImageGetter |
tagHandler | Html.TagHandler |
Spanned |
fromHtml
Added in API level 24
public static Spanned fromHtml (String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
source | String |
flags | int |
imageGetter | Html.ImageGetter |
tagHandler | Html.TagHandler |
Spanned |
fromHtml
Added in API level 1
Deprecated in API level 24
This method was deprecated in API level
24.
use fromHtml(java.lang.String, int) instead.
Returns displayable styled text from the provided HTML string with the legacy flags FROM_HTML_MODE_LEGACY.
source | String |
Spanned |
toHtml
Added in API level 24
public static String toHtml (Spanned text, int option)Returns an HTML representation of the provided Spanned text. A best effort is made to add HTML tags corresponding to spans. Also note that HTML metacharacters (such as "<" and "&") within the input text are escaped.
text | Spanned: input text to convert |
option | int: one of TO_HTML_PARAGRAPH_LINES_CONSECUTIVE or TO_HTML_PARAGRAPH_LINES_INDIVIDUAL |
String | string containing input converted to HTML |
toHtml
Added in API level 1
Deprecated in API level 24
This method was deprecated in API level
24.
use toHtml(android.text.Spanned, int) instead.
text | Spanned |
String |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2022-02-10 UTC.
[{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }]