i'm trying to change some items text color (or backgroung color) in a list view based on a flag . after a long search i didn't find out how to do it , i'm calling the below loop after specific action to change the color:
ListView _listView = (ListView) findViewById(R.id.listView2);
for (int i=0;i<=_listView.getCount();i++)
{
if(Falg == True)
{
//here i want to change the list view item color or backgroud color
}
}
xml version = "1.0" encoding = "utf-8" ?> Below is the code for the Item layout for displaying the item in the ListView. We have added textColor and textSize attributes to the TextView to change the text color and size.
layout. simple_list_item_1 , which is a layout built into Android that provides standard appearance for text in a list, and an ArrayList called restaurants (not seen here).
You can use this adapter to provide views for an AdapterView , Returns a view for each object in a collection of data objects you provide, and can be used with list-based user interface widgets such as ListView or Spinner .
You can override the getView method of Array adapter and change the color:
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1,
myList) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
if (flag == True) {
text.setTextColor(Color.BLACK);
}
return view;
}
};
You can do it directly in your custom Adapter
.
See Adapter.getView()
You can inflate row layout in this method and dynamically change view colors and other stuff.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With