Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Display HTML in a ListView

Sorry if this is obvious to everyone else but I am having a minor difficulty understanding how to display html inside my listview.

My list view is declared.

ListView lv1 = (ListView) findViewById(R.id.ListView01);

I populate it (not shown) then set my listview here with an ArrayAdapter.

lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, foods));

Further down I create a new array of strings that I want to have bold tags in. I then add this new array (called arr_sort) to the arrayadapter insdie a onTextChanged() method.

lv1.setAdapter(new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort));

So now that my new Array of Strings has < b > tags in it. How do I make my listview display the bold text?

Here is my new_list_view

    <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:textColor="@color/grey2"
    android:textSize="20sp"
    android:gravity="center_vertical"
    android:paddingLeft="6dip"
    android:minHeight="40dip"
/> 

And here is my ListView part in my main layout.

        <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corners_green"
        android:cacheColorHint="#00000000"
        android:divider="@color/green6"
        android:dividerHeight="1px"
        android:fastScrollEnabled="true" >
    </ListView>

Any help would be much appreciated.

like image 634
S.A.Jay Avatar asked Mar 13 '12 04:03

S.A.Jay


4 Answers

Ok, so Jitendra Sharma was had the right idea for my scenario, but I needed to override the getView method. Or at least that is what worked for me. Then in the getView method I was able to set my text to render in html.

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort)
            {
                @Override
                public View getView(int position, View convertView, ViewGroup parent) 
                {
                    View row;

                    if (null == convertView) {
                    row = mInflater.inflate(R.layout.new_list_view, null);
                    } else {
                    row = convertView;
                    }

                    TextView tv = (TextView) row.findViewById(android.R.id.text1);
                    tv.setText(Html.fromHtml(getItem(position)));
                    //tv.setText(getItem(position));

                    return row;
                }

            };
            lv1.setAdapter(adapter);
like image 160
S.A.Jay Avatar answered Nov 15 '22 05:11

S.A.Jay


override getItem method of the Adapter and do the following:

ArrayAdapter<String> adapter= ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort){
     public Object getItem(int position)
     {
          return Html.fromHtml(arr_sort.get(position));
     }
};
like image 35
jeet Avatar answered Nov 15 '22 04:11

jeet


If you are using a SimpleAdapter, here is the code that enables HTML on a TextView.

adapter.setViewBinder(new SimpleAdapter.ViewBinder() {  
    public boolean setViewValue(View view, Object data, String textRepresentation) {  
        if (data instanceof Spanned && view instanceof TextView) {  
            ((TextView) view).setText((Spanned) data);  
        } else {  
            ((TextView) view).setText(Html.fromHtml(String.valueOf(data))); 
        }  
        return true;  
        }  
    }  
); 

Ref: [Link] (http://android.jreactor.com/2012/07/17/simpleadapter-spanned-html-fromhtml/)

like image 42
Alfred Avatar answered Nov 15 '22 03:11

Alfred


If all you wanted is to display some text where parts of the text should be bold, all you need is one TextView, and properly formatted text (with <b> added) and do the following:

textview.setText(Html.fromHtml(text));

For more information on what TextView+Html can support, see here

like image 38
Kai Avatar answered Nov 15 '22 05:11

Kai