Question

I want to Highlight (change color) a single word in a string and then after some random time I want to change the next word color.

I know how to change color of sub string but I want to do at run time and keep changing the color of next word.

Plese help

`@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    String coloredString;
    String myStr[] = myStrList.get(position).split("-");

    for(int i=0; i<myStr.length;i++){

         if(position==selectionRowIndex){
             if(i==selectionWordIndex){
                 String colorWord = "<font color='#EE0000'>"+myStr[i]+"</font> ";
                 coloredString+=colorWord;
             }else{
                 coloredString+=myStr[i];
             } 
         }else{
             coloredString+=myStr[i];
         }

    }

     ViewHolder holder = null;
     LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

     if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();


            holder.txtStr = (TextView) convertView.findViewById(R.id.text_Str);


            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();




            holder.txtStr .setText(Html.fromHtml(coloredString));
Was it helpful?

Solution

Create a Spannable object, set its text via SpannableString.

Spannable spanobj = new SpannableString("word to set color");        
spanobj.setSpan(new ForegroundColorSpan(Color.WHATEVER), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

You can use this to set the text on your textview.setText(spanobj);

Cycle the 0, 4 (start index, end index) to say 5, 7 and update the view to color more of the string. 0,4 would set the color of 'word' 5,7 should set the color of to and so on...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top