I have the following input in a String: "Hello # this # is # sample # text."
It sets the background color for all elements between # characters. Here is what I got so far:
public static CharSequence colorBackground(CharSequence text) { Pattern pattern = Pattern.compile("#(.*?)#"); Spannable spannable = new SpannableString( text ); if( pattern != null ) { Matcher matcher = pattern.matcher( text ); while( matcher.find() ) { int start = matcher.start(); int end = matcher.end(); CharacterStyle span = new BackgroundColorSpan(0xFF404040); spannable.setSpan( span, start, end, 0 ); } } return spannable; }
Setting the background color works, but the placeholders # are also stylized. How to delete them before returning the result, because the ReplaceAll method does not exist for CharSequence?
I use this function for TextView line styles in ListView. After adding this style function, it is a little slower in the emulator. Maybe I should approach him in some other way, for example. use custom textview with custom drawing function?
joshas
source share