Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change listview item text color

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
           }
        }
like image 989
Mohammad abumazen Avatar asked Dec 27 '13 23:12

Mohammad abumazen


People also ask

How do you change the color of text in list view?

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.

What is simple_list_item_1?

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).

What is the use of adapter object in Android SDK explain Arrayadapter in detail?

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 .


2 Answers

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;
    }
};
like image 158
Adnan Mulla Avatar answered Sep 28 '22 06:09

Adnan Mulla


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.

like image 41
pawegio Avatar answered Sep 28 '22 06:09

pawegio