Inconsistent SpannableString setSpan() coloring?

696 views Asked by At

I'm trying to color only vowels in my String 1 color (ex red), and non-vowels another (ex: blue). But the SpannableString setSpan() method is being inconsistent when iterating thru each char. The function is detecting the vows and non-vows correctly, as I had checked the logged output,except that the coloring is not correct:

//ColorLogic.java:
public SpannableString colorString(String myStr)
    {
        SpannableString spnStr=new SpannableString(myStr);
        ForegroundColorSpan vowColor=new ForegroundColorSpan(Color.RED);
        ForegroundColorSpan conColor=new ForegroundColorSpan(Color.BLUE);
        int strLen=myStr.length();
        for(int i=0; i< strLen; i++)
        {
            if (vowSet.contains(Character.toLowerCase(myStr.charAt(i))))
            //if (i%2==0)
            {
                 Log.v(DTAG, "vow"+myStr.charAt(i));
                 spnStr.setSpan(vowColor, i, i, 0);

            }
            else
            {
                Log.v(DTAG, "cons"+myStr.charAt(i));
                spnStr.setSpan(conColor, i, i, 0);
            }
        }
        return spnStr;
    }

    //In my OnCreate of my activity class:
    //PASS
     //Log.v(DTAG, message);
     // Create the text view
     TextView textView = new TextView(this);
     textView.setTextSize(50);

     //Call Color Logic to color each letter individually
     ColorLogic myColorTxt=new ColorLogic();
     SpannableString spnMsg=myColorTxt.colorString(message);
     //Log.v(DTAG, "spnMsg: "+spnMsg.toString());

     textView.setText(spnMsg, BufferType.SPANNABLE);
    //textView.setTextColor(Color.GREEN);
     setContentView(textView);
     }


      ![Vows Only its correct (non-vowels only is correct as well)][1]
          ![With cons and vows, 2 letters then its incorrect!][2]

Alternating vows and cons is incorrect as well

1

There are 1 answers

2
CommonsWare On

You cannot reuse span objects. As pskink indicates, please use distinct ForegroundColorSpan objects for each setSpan() call.

Also, you may wish to use fewer spans overall. While your sample ("abibobu") requires the maximum possible number of spans, most words have consonants and vowels strung together. For example, the word "consonant" has two two-consonant spans ("ns" and "nt"). Those could be colored using a single ForegroundColorSpan, rather than two, improving rendering speed. Spans are easy but not the fastest, and so the fewer spans you use, the better your app will perform, particularly in animated situations (e.g., scrolling in a ListView).

Furthermore, you may only need to color either consonants or vowels, unless you are planning on a third color for hyphens and apostrophes. Remember: your text can start with a color (e.g., android:textColor).