dynamically change TextView font color in ListView

I am binding an XML document to a custom adapter. All of the items in the list initially have a font color of white. One of the nodes in the XML document has an attribute that I am checking and if the attribute is set, I’d like to change the font color of that item in the ListView to a darker color. The code I have seems to work initially, but if I scroll the list up and down, the items in the ListView that should stay white, change automatically to the dark color, for some reason:

Here is my layout code:

  • using both setOnItemClickListener and setOnItemLongClickListener both listener fired same time or works same time.Why
  • How to get ActionBar view?
  • Comparing two locations using their Longitude and Latitude
  • Activity onStop() not called when home button is pressed in Android N multi window mode
  • Copy text from TextView on Android
  • How to debug the Android App in release mode using Android studio
  •  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:orientation="vertical">
     <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:textSize="15dip" 
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:padding="5px"
         />
     </LinearLayout>
    

    and here is my custom adapter:

     private class CustomAdapter extends BaseAdapter 
     {       
        private ArrayList<FilterItem> mData = new ArrayList<FilterItem>();
        private LayoutInflater mInflater;
    
        public CustomAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    
        public void addItem(FilterItem item) {
            mData.add(item);
        }
    
        @Override
        public int getCount() {
            return mData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder;
            FilterItem item = (FilterItem)this.getItem(position);
    
            if (convertView == null)
            {
    
                holder = new ViewHolder();
    
                convertView = mInflater.inflate(R.layout.main, null); 
                holder.text = (TextView)convertView.findViewById(R.id.text);
    
                convertView.setTag(holder);
    
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
    
                TextView tvText = holder.text;
    
                tvText.setText(item.getTitle());
    
                if (item.Read())
                {
                    tv.setTextColor(Color.Gray);
                }
    
            return(convertView);
        }
    }
    

    Related posts:

    AppCompat Toolbar: Change Overflow Icon Color in ActionMode
    Linux rt- patch for android anyone?
    where does ansroid.os.SystemProperties store its key/values?
    Android - Google Spreadsheet Api
    Android BroadcastReceiver onReceive Update TextView in MainActivity
    FTDI D2XX Library open multi interface devices using openByUSBDevice() call
  • How to get all android contacts but without those which are on SIM
  • Android: Vertical alignment for multi line EditText (Text area)
  • Memory leaked (activity leak) caused by CookieSyncManager?
  • Why does Android Studio always show ActionBar in app design, even when disabled?
  • Prevent dialog dismissal on screen rotation in Android
  • CollapsingToolbarLayout subtitle
  • 2 Solutions collect form web for “dynamically change TextView font color in ListView”

    ListView reuses view, as you can see in getView you get convertView, and you only change color from white to gray. You never restore color back to white. I suggest

    if (item.Read()) {
        tv.setTextColor(Color.Gray);
    } else {
        tv.setTextColor(Color.White);
    }
    

    Move the text and color setting code to before setting the tag. Try this code

     if (convertView == null)
        {
    
            holder = new ViewHolder();
    
            convertView = mInflater.inflate(R.layout.main, null); 
            holder.text = (TextView)convertView.findViewById(R.id.text);
            TextView tvText = holder.text;
            tvText.setText(item.getTitle());
            if (item.Read())
            {
               tv.setTextColor(Color.Gray);
            }
            convertView.setTag(holder);
    
        } else {
            holder = (ViewHolder)convertView.getTag();
    
            TextView tvText = holder.text;
            tvText.setText(item.getTitle());
            if (item.Read())
            {
                tv.setTextColor(Color.Gray);
            }
        }
        return(convertView);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.