최근에 TextView에 TEXT를 넣으면서 정렬문제로 여러가지 고민을 하다가 발견한 문서다.
중요한 것은 이미지를 보여줄 수도 있고, 링크도 제공할 수 있다는 것이다.. ㅎ;
이미지의 경우 웹서버에서 불러오는 것이 아니라 앱 리소스에서 제공하는 이미지를 매칭하여 보여준다.
기타 아래 박스에 보면 사용 가능한 TAG들이 정리되어 있다~
확인해 보자!
This example explains to display HTML in Android TextView. Many times while you design an application, you may encounter a place where you will like to use HTML content in your screen. This may be to display a static “eula” or “help” content. In android there is a lovely class android.text.HTML
that processes HTML strings into displayable styled text. Currently android doesn’t support all HTML tags.
Android API documentation does not stipulate what HTML tags are supported. I have looked into the android Source code and from a quick look at the source code, here’s what seems to be supported as of now.
<a href="..."> <b>, <big>, <blockquote>, <br>, <cite>, <dfn> <div align="...">, <em>, <font size="..." color="..." face="..."> <h1>, <h2>, <h3>, <h4>, <h5>, <h6> <i>, <img src="...">, <p>, <small> <strike>, <strong>, <sub>, <sup>, <tt>, <u>
From HTML method returns displayable styled text from the provided HTML string. As per android’s official Documentations any tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
Html.formHtml
method takes an Html.TagHandler
and an Html.ImageGetter
as arguments as well as the text to parse. We can parse null
as for the Html.TagHandler but you’d need to implement your own Html.ImageGetter
as there isn’t a default implementation. The Html.ImageGetter
needs to run synchronously and if you’re downloading images from the web you’ll probably want to do that asynchronously. But in my example I am using the images from resources to make my ImageGetter implementation simpler.
Display HTML in Android TextView- Example
main_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <TextView android:id="@+id/title_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Your HTML text Below" /> <TextView android:id="@+id/html_text" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
MainActivity.java
package com.javatechig.example.ui; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.Drawable; import android.text.Html; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private final String htmlText = "<body><h1>Heading Text</h1><p>This tutorial " + "explains how to display " + "<strong>HTML </strong>text in android text view. </p>" + "<img src=\"hughjackman.jpg\">" + "<blockquote>Example from <a href=\"www.javatechig.com\">" + "Javatechig.com<a></blockquote></body>"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView htmlTextView = (TextView)findViewById(R.id.html_text); htmlTextView.setText(Html.fromHtml(htmlText, new ImageGetter(), null)); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private class ImageGetter implements Html.ImageGetter { public Drawable getDrawable(String source) { int id; if (source.equals("hughjackman.jpg")) { id = R.drawable.hughjackman; } else { return null; } Drawable d = getResources().getDrawable(id); d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight()); return d; } }; }
출처: http://javatechig.com/android/display-html-in-android-textview
'개발관련 정보 > 안드로이드' 카테고리의 다른 글
메소드 버스 활용하기 (0) | 2019.04.22 |
---|---|
5.0 롤리팝에서 숫자 콤마 표현 (0) | 2015.04.22 |
구글 애드몹(AdMob) 광고달기 (0) | 2014.12.22 |
Pinterest 공유걸기~ (0) | 2014.12.15 |
리얼클릭 레몬(RemoN) SDK를 달아보자 (0) | 2014.12.12 |