Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

Injecting html

pyricau edited this page Jan 14, 2012 · 8 revisions

Since AndroidAnnotations 2.2

If you want to inject HTML text in a TextView (may it be to format it or because you love HTML), there are two annotations that can help you:

  • @FromHtml
  • @HtmlRes

Let's say you have the following string resource:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_html"><![CDATA[Hello <b>World</b>!]]></string>
</resources>

@HtmlRes

This annotation acts as @StringRes (retrieves a String resource) and wraps the result with a call to HTML.fromHtml():

@EActivity
public class MyActivity extends Activity {

  // Injects R.string.hello_html
  @HtmlRes(R.string.hello_html)
  Spanned myHelloString;

  // Also injects R.string.hello_html
  @HtmlRes
  CharSequence helloHtml;

}

Note that Spanned implements CharSequence, thus you can use both for a @HtmlRes.

@FromHtml

This annotation must be used on a TextView already annotated with @ViewById. The purpose of this annotation is to set HTML text in a TextView:

@EActivity
public class MyActivity extends Activity {

  @ViewById(R.id.my_text_view)
  @FromHtml(R.string.hello_html)
  TextView textView;

  // Injects R.string.hello_html into the R.id.hello_html view
  @ViewById
  @FromHtml
  TextView helloHtml;

}

Using AndroidAnnotations

Questions?

Enjoying AndroidAnnotations

Improving AndroidAnnotations

Extending AndroidAnnotations

Clone this wiki locally