Anupam
Anupam

Reputation: 3752

How to make dynamic text bold having tag <b> </b> in listview?

I'm developing an application in which I'm parsing JSON data and fixing the result in the listview. In JSON response I'm getting the following response:

{
"searchdata": {
    "webresult": [
        {
            "title": "<b>Android</b>",
            "desc": "Discover a new flavor of Jelly Bean <b>Android</b> 4.2 introduces a completely new camera experience, a new form of typing that helps you power ...",
            "link": "http://www.android.com/"
        },
        {
            "title": "<b>Android</b> (operating system) - Wikipedia, the free encyclopedia",
            "desc": "<b>Android</b> is a Linux -based operating system designed primarily for touchscreen mobile devices such as smartphones and tablet computers. Initially developed by <b>Android</b> ...",
            "link": "http://en.wikipedia.org/wiki/Android_(mobile_phone_platform)"
        }
    ]
}

}

In title as you can see Android tags are coming. I want to make it bold.

Here is what I'm doing for fixing those response in the listview.

try {
    HttpClient hClient = new DefaultHttpClient();
HttpGet hGet = new HttpGet("MY API");
ResponseHandler<String> rHandler = new BasicResponseHandler();
data = hClient.execute(hGet, rHandler);

JSONObject rootObj = new JSONObject(data);

JSONObject jSearchData = rootObj.getJSONObject("searchdata");

JSONArray jsonArray = jSearchData.getJSONArray("webresult");

for (int i = 0; i < jsonArray.length(); i++) {

    JSONObject objJson = jsonArray.getJSONObject(i);

    String title = objJson.getString("title");
    String desc = objJson.getString("desc");
    String link = objJson.getString("link");

    HashMap<String, String> map = new HashMap<String, String>();

    map.put("title", title);
    map.put("description", desc);
    map.put("link", link);

    searchList.add(map);
    }

} catch (Exception e) {
       System.out.println("Exception: " + e);
}

By the above code I'm getting the response and storing them into Hashmap. After that I'm using custom layout for displaying the content. Following code helps me getting the values fixed in the layout.

ListAdapter adapter = new SimpleAdapter(getApplicationContext(),
searchList, R.layout.list_row, new String[] { "title","description", "link" }, new int[] { R.id.title,R.id.desc, R.id.url });

setListAdapter(adapter);

Here, is what it looks like when all things go well.

Screenshot from device of the layout

As you can see the html tags are coming. Can please anyone suggest how to make the text bold inside the html bold tags?

Any help will be appreciated for this.

Upvotes: 0

Views: 2326

Answers (4)

android_new_geek
android_new_geek

Reputation: 11

Just use SpannedString instead of normal string and then display it on TextView using setText().

Example: SpannedString spannedText = new SpannedString(Html.fromHtml(value)); textView.setText(spannedText);

Upvotes: 1

Vladimir Mironov
Vladimir Mironov

Reputation: 30874

You can use SimpleAdapter.ViewBinder and set a html value to the TextView instead of plain text.

SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), searchList, R.layout.list_row, new String[] { "title","description", "link" }, new int[] { R.id.title,R.id.desc, R.id.url });
SimpleAdapter.ViewBinder binder = new SimpleAdapter.ViewBinder() {
    @Override
    public boolean setViewValue(View view, Object object, String value) {
        if (view instanceof TextView) {
            ((TextView) view).setText(Html.fromHtml(value));
            return true;
        }

        return false;
    }
};

adapter.setViewBinder(binder);
setListAdapter(adapter);

Upvotes: 4

Ramachandra Reddy Avula
Ramachandra Reddy Avula

Reputation: 1114

In Adapter class while setting the title just try as fallow.

holder.title.setText(Html.fromHtml(searchList.getTitle)).

Upvotes: 0

Yasitha Waduge
Yasitha Waduge

Reputation: 13540

Use Html.fromHtml() before you put in to Map

map.put("title", HTML.fromHtml(title));
map.put("description", HTML.fromHtml(desc));
map.put("link", HTML.fromHtml(link));

Upvotes: 2

Related Questions