I am developing an android app, and in one fragment, I already have a listview and have a set of items that will be added to this listview via an arrayadapter. However, I was requested to dynamically color some list items based on a particular property in the original database, specifically bold only those elements to highlight them. Is there any possible way to acheve this with the arrayAdapter class, or if not, considering this is a simplistic use case, what can I do to achieve this
-
you can check for particular criteria for styling in public View getView(int position, View convertView, ViewGroup parent) method. – Sandeep Dec 22 '16 at 07:30
4 Answers
It's very easy to handle this problem you just have to put a condition in your adapter and if that condition is true you just have to customize your item as it's asked from you .
if(condition)
{
view.setBackgroundColor(activity.getResources().getColor(R.color.White));
}
And if you want to set a TextView
as Bold programmatically in your adapter check your condition and set it like below :
textView.setTypeface(null, Typeface.BOLD);

- 1,129
- 1
- 12
- 33
you can do it by your ArrayAdapter class, just put you condition in adapter getView Method, and set your text view as a bold
if(conditionPart){
textView.setTypeface(null, Typeface.BOLD); // For bold
textView.setBackgroundColor(context.getResources().getColor(R.color.Black)); // For Black Color Background
}else{
textView.setTypeface(null, Typeface.NORMAL);// For Noral
textView.setBackgroundColor(context.getResources().getColor(R.color.Gray)); // For Gray Color Background
}

- 1,367
- 1
- 11
- 17
Refer to this tutorial: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView
Extend ArrayAdapter class and override getView() method
Apply conditional statement as mentioned above

- 449
- 7
- 17
You can create your custom Adapter
where you can style your text as you want here is sample for custom adapter:
Custom Adapter for List View

- 1
- 1

- 467
- 1
- 6
- 14