Create Compound Spannable Text from Character Style - Android android.text

Android examples for android.text:SpannableString

Description

Create Compound Spannable Text from Character Style

Demo Code

import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.style.CharacterStyle;

public class Main {

  public static Spannable getCompoundText(String str0, String str1, CharacterStyle style1) {
    return getCompoundText(new String[] { str0, str1 }, new CharacterStyle[] { null, style1 });
  }/*from w w w.  j a va  2  s .c  o  m*/

  public static Spannable getCompoundText(String[] strs, CharacterStyle[] styles) {
    SpannableStringBuilder strTarget = new SpannableStringBuilder();
    int len = strs.length;
    for (int i = 0; i < len; i++) {
      SpannableString str = new SpannableString(strs[i]);
      str.setSpan(styles[i], 0, str.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
      strTarget.append(str);
    }
    return strTarget;
  }

}

Related Tutorials