9

I need an example of how to display the strings that I have marked up with simple html into a TextView. I have found "Spanned fromHtml(String source)", but I don't know how to plug it into my java code.

Here is my Java:

package com.SorenWinslow.TriumphHistory;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class TriumphHistory extends ListActivity {
    String[] HistoryList;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayAdapter<String> adapter;
        HistoryList = getResources().getStringArray(R.array.history);
        adapter = new ArrayAdapter<String> (this,R.layout.historylistlayout,HistoryList);
        setListAdapter(adapter);

    }
}

Here is a sample of history:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="history">
<item><b>1883</b><br/>Some stuff happened</item>
<item><b>1884</b><br/>Some more stuff happened <i>before</i> the other stuff
</item>
<resources>

Here is my historylistlayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_vertical"
    android:textColor="#ffffff"
    android:background="#000050"
    android:layout_marginTop="5px"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:padding="3px" 
    android:textSize="8pt" android:layout_gravity="top|left"/>

And here is my main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:textColor="#ffffff"
    android:background="#000080" 
    android:isScrollContainer="true" 
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent" android:scrollbarStyle="insideOverlay">

  <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:dividerHeight="1px"/>

</LinearLayout>
ariefbayu
  • 21,849
  • 12
  • 71
  • 92
Soren
  • 1,737
  • 5
  • 15
  • 11

5 Answers5

13

I was trying to do something of the same nature and found out that my html along with CSS was not getting formatted correctly so I took the string and loaded it into a webview like this:

 WebView webview = (WebView) findViewById(R.id.MyWebview);
 String summary = "<html><body>You scored <b>192</b> points.</body></html>";
 webview.loadData(summary, "text/html", "utf-8");

and it recognized all the styles and formatted the html correctly. More from the android reference HERE

ninjasense
  • 13,756
  • 19
  • 75
  • 92
12

Use the function you mentioned to return a spanned and then set the text of your textview using the toString of the returned Spanned object like so to achieve formatted text:

Spanned marked_up = Html.fromHtml(yourTextWithHTML);
((TextView) findViewById(R.id.text1)).setText(marked_up.toString());

This will work where yourTextWithHTML is any of the Strings within the item tags you posted. eg "<b>1883</b><br/>Some stuff happened"

jqpubliq
  • 11,874
  • 2
  • 34
  • 26
  • 1
    This works if you're inflating a String resource, but not if you're inflating a String array. String arrays will remove HTML formatting. – Jason Robinson Mar 19 '12 at 16:54
8
    Spanned spannedContent = Html.fromHtml(htmlString);
    textView.setText(spannedContent, BufferType.SPANNABLE);
shiami
  • 7,174
  • 16
  • 53
  • 68
1

This seems easiest actually (from HTML in string resource?):

mTextView.setText(getText(R.string.my_styled_text));

This works for strings that have HTML inside.

Community
  • 1
  • 1
Artem Russakovskii
  • 21,516
  • 18
  • 92
  • 115
0

I faced the same problem, and solved it by using

    CharSequence[] HistoryList = getResources().getTextArray(R.array.history);

The above code returns styled text array associated with resource. It will return CharSequence[]

Then just use the elemens in

   setText(HistoryList[index]);
Simon
  • 226
  • 3
  • 13