I might be misunderstanding some restrictions or subtlety about the
use of Spannables in a TextView.

It seems that whenever I apply an identical combination of display
enhancements to two separate and non-overlapping ranges of text, the
first set (earlier in the string) does not take effect in the actual
display.

For example, if I make characters 0-3 foreground blue, and also
characters 12-14 foreground blue, then only characters 12-14 appear
blue.  But if I use a different combination of enhancements for the
second range, say, blue foreground with white background, the all of
the enhancements appear in both ranges as expected.

I hope the problem is in my own code, but I'm not seeing it, and I'm
suspecting that there is some consolidation at a lower level occurring
that is going awry.  Here is a class I use to apply this stuff:

class EnhTextBuilder {
    public static final String TAG = "ETB";
    StringBuilder sbb;
    Vector<Enote> enhVec; // hold enhancements

    EnhTextBuilder () {
        sbb = new StringBuilder();
        enhVec = new Vector<Enote>(); // hold enhancements
    }
    private class Enote {
        int pos, len;
        Object enh;
        Enote (int p, int l, Object e) {
            pos = p; len = l; enh = e;
        }
    }
    void append (String s) {
        Log.d(TAG,"ETB text:"+s);
        sbb.append(s);
    }
    void append (String s, Object e) {
        Log.d(TAG,"ETB E:"+e);
        enhVec.add(new Enote(sbb.length(),s.length(),e));
        append(s);
    }
    void append (String s, Object[] e) {
        for (int i = 0; i < e.length; i++) {
            Log.d(TAG,"ETB E:"+e[i]);
            enhVec.add(new Enote(sbb.length(),s.length(),e[i]));
        }
        append(s);
    }
    public String toString () { return sbb.toString(); }

    // Put text into TextView and apply accumulated span
enhancements...
    public void setText (TextView tv) {
        tv.setText(sbb.toString());
        Spannable sText = (Spannable)tv.getText();
        //Log.d(TAG,"Spannable len:"+sText.length());
        for (Iterator ei = enhVec.iterator(); ei.hasNext(); ) {
            Enote e = (Enote)ei.next();
            //Log.d(TAG,"Enh P: "+e.pos+" for "+e.len);
            sText.setSpan(e.enh,e.pos,e.pos+e.len,0);
        }
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to