Android Display Html In A Listview
Solution 1:
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 = newArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort)
{
@Overridepublic 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;
}
TextViewtv= (TextView) row.findViewById(android.R.id.text1);
tv.setText(Html.fromHtml(getItem(position)));
//tv.setText(getItem(position));return row;
}
};
lv1.setAdapter(adapter);
Solution 2:
override getItem method of the Adapter and do the following:
ArrayAdapter<String> adapter= ArrayAdapter<String>(SearchByFood.this, R.layout.new_list_view, arr_sort){
publicObjectgetItem(int position)
{
returnHtml.fromHtml(arr_sort.get(position));
}
};
Solution 3:
If you are using a SimpleAdapter, here is the code that enables HTML on a TextView.
adapter.setViewBinder(newSimpleAdapter.ViewBinder() {
publicbooleansetViewValue(View view, Object data, String textRepresentation) {
if (data instanceofSpanned && view instanceofTextView) {
((TextView) view).setText((Spanned) data);
} else {
((TextView) view).setText(Html.fromHtml(String.valueOf(data)));
}
returntrue;
}
}
);
Ref: [Link] (http://android.jreactor.com/2012/07/17/simpleadapter-spanned-html-fromhtml/)
Solution 4:
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
Solution 5:
If you have the possibility of loading your texts from strings.xml, adding the tag there will automatically bold your text.
If however your texts are dynamic, you will have to create a custom adapter, and in it to set the text using textView.setText(Html.fromHtml(yourText));
Post a Comment for "Android Display Html In A Listview"