Reputation: 2607
I'm trying to create a simple ListView. the items shown on the ListView are just strings from an array i have. Only that i want just one of the strings to be shown in bold. At first when i used a long TextView that contained all of the strings with line breaks tags inside a ScrollView, i could simply use
<b> </b>
around the string i wanted to be shown bold on the gui, this way:
TextView tv.setText(Html.fromHtml(str));
But using a big TextView inside a ScrollView caused other problems, and seemed a bad option when you have ListView exactly for this matter. So now I use a ListView, that gets all the strings from an ArrayList and displays them. Only that however I try to turn this around, I can't seem to change the style of one item into Bold. I also tried creating a TextView for each of the strings, and than add the TextViews to the List, but I don't know if ArrayAdapter or SimpleAdapter can do this. SimpleAdapter only seems to be able to get the TextView as a resource ID, which is the same for all items in the list, which prevents me from achieving what i want.
Long story short: after researching this issue here I know there are ways of extending BaseAdapter for a custom Adapter that allows any custom layout for the list, but is there really no other way? Do i really need to implement a BaseAdapter and @override getItem only to make one item bold? This seems odd to me. Does anyone know of a way I can just add TextViews to the ListView? using an ArrayAdapter displays the TextViews toString :/
If you know another simple way to show just one bold item, I'll really appriciate any help. Thanks!
Upvotes: 2
Views: 6535
Reputation: 1177
Inside the getView
function, make a reference to the TextView
like:
text = (TextView) convertView.findViewById(R.id.textBox);
and then:
text.setTypeface(null, Typeface.BOLD);
and you're done.
Upvotes: 1
Reputation: 301
Overwrite the getView(...) of your ArrayAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
View returnedView = super.getView(position, convertView, parent);
if (position == specialPosition) {
TextView text = (TextView) returnedView.findViewById(R.id.text);
text.setTypeface(null, Typeface.BOLD);
}
return returnedView;
}
Upvotes: 1
Reputation: 2607
Alright, I have finally decided to solve this by making a custom adapter which extends ArrayAdapter. There are many examples out there, but some of them provide a lot of extras and over complicate this issue in my opinion, and I just wanted to solve this decently. I found this example extremely useful and friendly, in my research here i saw a lot of developers asking similar questions, so I really hope this helps you too.
An adapter that extends ArrayAdapter isn't too complicated, but moreover allows you to have it exactly your way with the layout in a fairly simple way. After the adapter is done, you can just add html tags to your specific strings in your source array, and when you bind them on the Adapter, use Html.fromHtml . This is how I solved it.
If anybody knows if there's a way to make bold or colored text in different specific items on a ListView without the need to make this adjustment yourself (extending ArrayAdapter and overriding getItem()) please do share it with us.
I hope this helps others.
Upvotes: 5