Skip to content

Instantly share code, notes, and snippets.

@Sebring
Created September 4, 2015 09:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sebring/b86c00b4a0a09abbc14c to your computer and use it in GitHub Desktop.
Save Sebring/b86c00b4a0a09abbc14c to your computer and use it in GitHub Desktop.
HtmlTextView - Android TextView to in databinding where content is HTML
import android.content.Context;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.widget.TextView;
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);
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(Html.fromHtml(text.toString()), type);
this.setMovementMethod(LinkMovementMethod.getInstance());
}
}
@Sebring
Copy link
Author

Sebring commented Sep 4, 2015

This enhanced TextView is handy when using databinding and content is HTML.

entity:

public class HtmlItem {
    public String content;

    public HtmlItem(String htmlText) {
       content = htmlText;
    }
}

view:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable name="item" type="the.package.HtmlItem"/>
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

      <the.package.HtmlTextView
          android:layout_height="wrap_content"
          android:layout_width="match_parent"
          android:layout_gravity="top"
          android:text="@{item.content}"
        />
    </RelativeLayout>
</layout>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment