Skip to content

Instantly share code, notes, and snippets.

@aegis1980
Last active September 12, 2015 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aegis1980/b138dcb2fd1b2e98aa30 to your computer and use it in GitHub Desktop.
Save aegis1980/b138dcb2fd1b2e98aa30 to your computer and use it in GitHub Desktop.
Custom view subclass of Android TextView widget that allows you to use HTML string resources (for better localisation). Development of Millthorn's answer with CDATA tag in this stackoverflow question: http://stackoverflow.com/questions/1529068/is-it-possible-to-have-multiple-styles-inside-a-textview His(Her?) solution required you to programmati…
package com.fitc.views;
import android.annotation.TargetApi;
import android.content.Context;
import android.text.Html;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* Created by Jon on 3/01/2015.
*/
public class HtmlTextView extends TextView {
public HtmlTextView(Context context) {
super(context);
}
public HtmlTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(21)
public HtmlTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
public void setText(CharSequence s,BufferType b){
super.setText(Html.fromHtml(s.toString()),b);
}
}
<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"
android:orientation="vertical" >
<com.fitc.views.HtmlTextView
android:id="@+id/html_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/example_html" />
</LinearLayout>
<string name="example_html">
<![CDATA[
<b>Author:</b> Mr Donuthead<br/>
<b>Contact:</b> me@donut.com<br/>
<i>Donuts for life </i>
]]>
</string>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment