Development/Android

안드로이드 TextView에 HTML 태그 사용하기

Jamie 2021. 3. 5. 16:04
반응형

HtmlCompat.fromHtml(getString(R.string.string_kakao_warning_message), HtmlCompat.FROM_HTML_MODE_LEGACY);

 

안드로이드 개발하면서 TextView HTML 태그를 사용 할 일이 생겼습니다.

정확히는 텍스트안에 특정 텍스트는 색을 넣어야하는 상황이고, TextView 나눠서 사용하는것보단 TextView 태그로 변환해서 HTML 태그를 사용하는 방향으로 진행하였습니다.

 

안드로이드에서 Html 태그를 사용하는 방법은 다음과 같습니다.

Html.fromHtml(String, int) 메서드를 사용하고, API 24이상 (Android 7.0 Nought)에서 androidx HtmlCompat fromHtml(String, int) 메서드를 사용합니다.

Html.fromHtml(String, int) android.text 패키지에 기본적으로 존재하므로 라이브러리를 추가할 필요는 없으며, HtmlCompat 또한 androidx 일반적으로는 프로젝트 생성시 추가가 되어있으므로 바로 사용할 있지만, 없을경우에는 gradle 다음과 같이 추가해줍니다.

 

dependencies {
	implementation 'androidx.core:core:1.0.1
}

사용법은 다음과 같습니다.

Html.fromHtml(String, int) HtmlCompat.fromHtml(String, int)

 

첫번째 파라미터에는 HTML 표현할 문자열 (HTML 태그가 포함된 문자열) 추가해주고, 두번째 파라미터에는 플래그입니다.)

 

플래그는 다음과 같이 사용할 있습니다.

Constants
int FROM_HTML_MODE_COMPACT
Flags for fromHtml(String, int, ImageGetter, TagHandler): Separate block-level elements with line breaks (single newline character) in between.
int FROM_HTML_MODE_LEGACY
Flags for fromHtml(String, int, ImageGetter, 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 <h1>~<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.

https://developer.android.com/reference/androidx/core/text/HtmlCompat#fromHtml(java.lang.String,%20int,%20android.text.Html.ImageGetter,%20android.text.Html.TagHandler)

 

 

예제코드는 다음과 같습니다.

string.xml 태그가 포함된 텍스트를 넣어서 다음과 같이 사용하고 있습니다.

 

[string.xml]

<string name="from_html_message"><font color='blue'>테스트</font> : 테스트입니다.<br>테스트입니다.<br></string>

 

[Code]

HtmlCompat.fromHtml(getString(R.string.from_html_message), HtmlCompat.FROM_HTML_MODE_LEGACY);
    <androidx.appcompat.widget.AppCompatTextView

        android:id="@+id/tv_from_html_message"

        android:text="@string/from_html_message"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content">

    </androidx.appcompat.widget.AppCompatTextView>

 

참고 URL

 

반응형