Example usage for android.text SpannableString setSpan

List of usage examples for android.text SpannableString setSpan

Introduction

In this page you can find the example usage for android.text SpannableString setSpan.

Prototype

public void setSpan(Object what, int start, int end, int flags) 

Source Link

Usage

From source file:Main.java

public static SpannableString backgroundColor(SpannableString sequence, List<String> color) {
    int hex = Color.parseColor(color.get(0));

    sequence.setSpan(new BackgroundColorSpan(hex), 0, sequence.length(), 0);
    return sequence;
}

From source file:Main.java

public static SpannableString formatTextSize(String txt, int size, int start, int end) {
    SpannableString spanString = new SpannableString(txt);
    AbsoluteSizeSpan span = new AbsoluteSizeSpan(size, true);
    spanString.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spanString;
}

From source file:fr.simon.marquis.secretcodes.util.Utils.java

public static SpannableString applyCustomTypeFace(CharSequence src, Context ctx) {
    SpannableString span = new SpannableString(src);

    span.setSpan(
            new CustomTypefaceSpan("",
                    RobotoTypefaceManager.obtaintTypeface(ctx, RobotoTypefaceManager.ROBOTOSLAB_REGULAR)),
            0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return span;//from  w  w w  . j  a v a 2 s  .  c  o  m
}

From source file:Main.java

public static void setTitle(TextView titleTv, int titleColor, int titleTextSize, int messageColor,
        int messageTextSize, CharSequence title, CharSequence message) {
    titleTv.setMinHeight(titleTextSize * 3);

    if (!TextUtils.isEmpty(title) && TextUtils.isEmpty(message)) {
        titleTv.setTextColor(titleColor);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, titleTextSize);
        titleTv.getPaint().setFakeBoldText(true);
        titleTv.setText(title);//from ww  w. j  ava  2s.co m
    } else if (TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) {
        titleTv.setTextColor(messageColor);
        titleTv.setTextSize(TypedValue.COMPLEX_UNIT_PX, messageTextSize);
        titleTv.setText(message);
    } else if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(message)) {
        SpannableString titleSs = new SpannableString(title + "\n" + message);
        titleSs.setSpan(new ForegroundColorSpan(titleColor), 0, title.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new AbsoluteSizeSpan(titleTextSize), 0, title.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        titleSs.setSpan(new ForegroundColorSpan(messageColor), title.length(), titleSs.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleSs.setSpan(new AbsoluteSizeSpan(messageTextSize), title.length(), titleSs.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleTv.setText(titleSs);
        titleTv.setLineSpacing(0.0f, 1.2f);
    } else {
        titleTv.setVisibility(View.GONE);
    }
}

From source file:Main.java

public static SpannableString getSmallCapsString(String input) {
    // values needed to record start/end points of blocks of lowercase letters
    char[] chars = input.toCharArray();
    int currentBlock = 0;
    int[] blockStarts = new int[chars.length];
    int[] blockEnds = new int[chars.length];
    boolean blockOpen = false;

    // record where blocks of lowercase letters start/end
    for (int i = 0; i < chars.length; ++i) {
        char c = chars[i];
        if (c >= 'a' && c <= 'z') {
            if (!blockOpen) {
                blockOpen = true;/*from w  w  w . ja  va 2s .  co m*/
                blockStarts[currentBlock] = i;
            }
            // replace with uppercase letters
            chars[i] = (char) (c - 'a' + '\u0041');
        } else {
            if (blockOpen) {
                blockOpen = false;
                blockEnds[currentBlock] = i;
                ++currentBlock;
            }
        }
    }

    // add the string end, in case the last character is a lowercase letter
    blockEnds[currentBlock] = chars.length;

    // shrink the blocks found above
    SpannableString output = new SpannableString(String.valueOf(chars));
    for (int i = 0; i < Math.min(blockStarts.length, blockEnds.length); ++i) {
        output.setSpan(new RelativeSizeSpan(0.8f), blockStarts[i], blockEnds[i],
                Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }

    return output;
}

From source file:Main.java

/**
 * Generate a SpannableString that styles a specific sub-sequence of a CharSequence object.
 *
 * @param sequence    The original text.
 * @param subSequence A sub-sequence of the original text.
 * @param styleSpan   The style to be applied to the sub-sequence.
 * @return A SpannableString with the style applied to only the sub-sequence of the original
 * text./*from   www .ja  v a 2s. c om*/
 */
public static SpannableString spanSubstring(CharSequence sequence, CharSequence subSequence,
        StyleSpan styleSpan) {
    SpannableString spannableString = new SpannableString(sequence);
    int startIndex = spannableString.toString().indexOf(subSequence.toString());
    int endIndex = startIndex + subSequence.toString().length();
    spannableString.setSpan(styleSpan, startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return spannableString;
}

From source file:cat.mvmike.minimalcalendarwidget.service.DayService.java

private static void setInstanceNumber(final Context context, final RemoteViews cellRv, final String dayOfMonth,
        final boolean isToday, final int found) {

    Symbol symbols = ConfigurationService.getInstancesSymbols(context);
    Character[] symbolArray = symbols.getArray();

    int max = symbolArray.length - 1;
    String symbol = String.valueOf(found > max ? symbolArray[max] : symbolArray[found]);
    String dayOfMonthSpSt = PADDING + (dayOfMonth.length() == 1 ? dayOfMonth + DOUBLE_PADDING : dayOfMonth)
            + PADDING + symbol;/*from  w w w .  j  a va2  s. c om*/
    SpannableString daySpSt = new SpannableString(dayOfMonthSpSt);
    daySpSt.setSpan(new StyleSpan(Typeface.BOLD), dayOfMonthSpSt.length() - 1, dayOfMonthSpSt.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    int color;
    if (isToday) {
        color = ContextCompat.getColor(context, R.color.instances_today);
        daySpSt.setSpan(new StyleSpan(Typeface.BOLD), 0, dayOfMonthSpSt.length() - 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        color = ContextCompat.getColor(context,
                ConfigurationService.getInstancesSymbolsColours(context).getHexValue());
    }

    daySpSt.setSpan(new ForegroundColorSpan(color), dayOfMonthSpSt.length() - 1, dayOfMonthSpSt.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    daySpSt.setSpan(new RelativeSizeSpan(symbols.getRelativeSize()), dayOfMonthSpSt.length() - 1,
            dayOfMonthSpSt.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    cellRv.setTextViewText(android.R.id.text1, daySpSt);
}

From source file:Main.java

public static void showChangelog(final Context context, final String title, final String appname,
        final int resChanges, final int resNotes) {
    final SharedPreferences p = PreferenceManager.getDefaultSharedPreferences(context);
    final String v0 = p.getString(PREFS_LAST_RUN, "");
    String v1 = null;/*from  ww  w  . j  a va2s  .  c o  m*/
    try {
        v1 = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        Log.e(TAG, "package not found: " + context.getPackageName(), e);
    }
    p.edit().putString(PREFS_LAST_RUN, v1).commit();
    if (v0.length() == 0) {
        Log.d(TAG, "first boot, skip changelog");
        // return;
    }
    if (v0.equals(v1)) {
        Log.d(TAG, "no changes");
        return;
    }

    String[] changes = context.getResources().getStringArray(resChanges);
    String[] notes = resNotes > 0 ? context.getResources().getStringArray(resNotes) : null;

    final SpannableStringBuilder sb = new SpannableStringBuilder();
    for (String s : notes) {
        SpannableString ss = new SpannableString(s + "\n");
        int j = s.indexOf(":");
        if (j > 0) {
            if (!TextUtils.isEmpty(s)) {
                ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
        sb.append(ss);
        sb.append("\n");
    }
    if (notes != null && notes.length > 0) {
        sb.append("\n");
    }
    for (String s : changes) {
        s = appname + " " + s.replaceFirst(": ", ":\n* ").replaceAll(", ", "\n* ") + "\n";
        SpannableString ss = new SpannableString(s);
        int j = s.indexOf(":");
        if (j > 0) {
            ss.setSpan(new StyleSpan(Typeface.BOLD), 0, j, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        sb.append(ss);
        sb.append("\n");
    }
    sb.setSpan(new RelativeSizeSpan(0.75f), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    changes = null;
    notes = null;

    AlertDialog.Builder b = new AlertDialog.Builder(context);
    b.setTitle(title);
    b.setMessage(sb);
    b.setCancelable(true);
    b.setPositiveButton(android.R.string.ok, null);
    b.show();
}

From source file:Main.java

public static SpannableString StringToBitMap(Context context, String msg) {
    SpannableString ss = new SpannableString(msg);
    Pattern p = Pattern.compile("/mnt/sdcard/.+?\\.\\w{3}");
    Matcher m = p.matcher(msg);/*from ww  w  . j  a  v a 2  s . co m*/
    while (m.find()) {
        Bitmap bitmap = BitmapFactory.decodeFile(m.group());
        ImageSpan imageSpan = new ImageSpan(context, bitmap);
        ss.setSpan(imageSpan, m.start(), m.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    return ss;
}

From source file:org.transdroid.core.gui.navigation.NavigationHelper.java

/**
 * Converts a string into a {@link Spannable} that displays the string in the Roboto Condensed font
 * @param string A plain text {@link String}
 * @return A {@link Spannable} that can be applied to supporting views (such as the action bar title) so that the input string will be displayed
 * using the Roboto Condensed font (if the OS has this)
 *///w  w  w.  ja  v a 2s.co  m
public static SpannableString buildCondensedFontString(String string) {
    if (string == null) {
        return null;
    }
    SpannableString s = new SpannableString(string);
    s.setSpan(new TypefaceSpan("sans-serif-condensed"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return s;
}