3

I have some contents which contains images and rich text format, and links which should work in android. How can i display them in android text view. and where i should store the content. What is the best practice ? use HTML content or break it in separate text and image views ?

For example i want to make a text view like the image below.

sample view

Abhigyan
  • 641
  • 10
  • 25

3 Answers3

3
String text = textView.getText().toString();
SpannableString spannableString = new SpannableString(text);

IconFontSpan iconFontSpan = new IconFontSpan(textView.getContext());
Pattern pattern = Pattern.compile("\u26F7");    // skier
Matcher matcher = pattern.matcher(text);
    while (matcher.find()) {
        spannableString.setSpan(iconFontSpan, 
        matcher.start(), matcher.end(), 0);
    }


private static class IconFontSpan 
    extends MetricAffectingSpan {
private static Typeface typeface = null;
public IconFontSpan(Context context) {
  if (typeface == null) {
    typeface = Typeface.createFromAsset(
        context.getAssets(), "icomoon.ttf");
  }
}
public void updateMeasureState(TextPaint textPaint) {
   textPaint.setTypeface(typeface);
}
public void updateDrawState(TextPaint textPaint) {
  textPaint.setTypeface(typeface);
}

enter image description here

Here is the link:

http://chiuki.github.io/advanced-android-textview/#/

johnrao07
  • 6,690
  • 4
  • 32
  • 55
0

Use HTML and display it in the android webview, instead of textview.

Mohit
  • 505
  • 8
  • 31
-1

You should store your HTML and all contents to the assets folder. Then load the html in the WebView. in your layout file write this code.

<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/>

and from your Activity you can set the html like this.

WebView webView= (WebView) findViewById(R.id.movieLoadingLoader); webView.loadUrl("file:///android_asset/your_html_folder/index.html");

Or,

If you want to load the HTML in your TextView , write this code .

Textview tv = (TextView) findViewById(R.id.textView); tv.setText(Html.fromHtml("<h4>Hello World</h4>"));

Zahidul Islam
  • 3,180
  • 1
  • 25
  • 35