Example usage for android.text SpannableStringBuilder setSpan

List of usage examples for android.text SpannableStringBuilder setSpan

Introduction

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

Prototype

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

Source Link

Document

Mark the specified range of text with the specified object.

Usage

From source file:Main.java

/**
 * Given a snippet string with matching segments surrounded by curly
 * braces, turn those areas into bold spans, removing the curly braces.
 *//*from  w  w w . j  a v a 2  s  .c  o m*/
public static Spannable buildStyledSnippet(String snippet) {
    final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);

    // Walk through string, inserting bold snippet spans
    int startIndex, endIndex = -1, delta = 0;
    while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
        endIndex = snippet.indexOf('}', startIndex);

        // Remove braces from both sides
        builder.delete(startIndex - delta, startIndex - delta + 1);
        builder.delete(endIndex - delta - 1, endIndex - delta);

        // Insert bold style
        builder.setSpan(new StyleSpan(Typeface.BOLD), startIndex - delta, endIndex - delta - 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //builder.setSpan(new ForegroundColorSpan(0xff111111),
        //        startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        delta += 2;
    }

    return builder;
}

From source file:Main.java

/**
 * Given a snippet string with matching segments surrounded by curly
 * braces, turn those areas into bold spans, removing the curly braces.
 *//*from   w  ww.j a  va  2 s  .c  o m*/
public static Spannable buildStyledSnippet(String snippet) {
    final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);

    // Walk through string, inserting bold snippet spans
    int startIndex = -1, endIndex = -1, delta = 0;
    while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
        endIndex = snippet.indexOf('}', startIndex);

        // Remove braces from both sides
        builder.delete(startIndex - delta, startIndex - delta + 1);
        builder.delete(endIndex - delta - 1, endIndex - delta);

        // Insert bold style
        builder.setSpan(sBoldSpan, startIndex - delta, endIndex - delta - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        delta += 2;
    }

    return builder;
}

From source file:de.ub0r.android.lib.DonationHelper.java

/**
 * Show "donate" dialog./*from  w  w  w  .  ja  v  a2 s  .c  o  m*/
 * 
 * @param context
 *            {@link Context}
 * @param title
 *            title
 * @param btnDonate
 *            button text for donate
 * @param btnNoads
 *            button text for "i did a donation"
 * @param messages
 *            messages for dialog body
 */
public static void showDonationDialog(final Activity context, final String title, final String btnDonate,
        final String btnNoads, final String[] messages) {
    final Intent marketIntent = Market.getInstallAppIntent(context, DONATOR_PACKAGE, null);

    String btnTitle = String.format(btnDonate, "Play Store");

    SpannableStringBuilder sb = new SpannableStringBuilder();
    for (String m : messages) {
        sb.append(m);
        sb.append("\n");
    }
    sb.delete(sb.length() - 1, sb.length());
    sb.setSpan(new RelativeSizeSpan(0.75f), 0, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    AlertDialog.Builder b = new AlertDialog.Builder(context);
    b.setTitle(title);
    b.setMessage(sb);
    b.setCancelable(true);
    b.setPositiveButton(btnTitle, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            try {
                context.startActivity(marketIntent);
            } catch (ActivityNotFoundException e) {
                Log.e(TAG, "activity not found", e);
                Toast.makeText(context, "activity not found", Toast.LENGTH_LONG).show();
            }
        }
    });
    b.setNeutralButton(btnNoads, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(final DialogInterface dialog, final int which) {
            try {
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(
                        "http://code.google.com/p/ub0rapps/downloads/list?" + "can=3&q=Product%3DDonator")));
            } catch (ActivityNotFoundException e) {
                Log.e(TAG, "activity not found", e);
                Toast.makeText(context, "activity not found", Toast.LENGTH_LONG).show();
            }
        }
    });
    b.show();
}

From source file:com.android.mail.browse.SendersView.java

private static void appendMessageInfo(SpannableStringBuilder sb, CharSequence text, Object span,
        boolean appendSplitToken, boolean convRead) {
    int startIndex = sb.length();
    if (appendSplitToken) {
        sb.append(sSendersSplitToken);//w  w w  .  j  ava  2s  .  c  o  m
        sb.setSpan(CharacterStyle.wrap(convRead ? sMessageInfoReadStyleSpan : sMessageInfoUnreadStyleSpan),
                startIndex, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    startIndex = sb.length();
    sb.append(text);
    sb.setSpan(span, startIndex, sb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:it.mb.whatshare.Dialogs.java

/**
 * Sad workaround./*ww w.  j  a v a  2 s  .  com*/
 * 
 * See http://stackoverflow.com/a/7350315/1159164
 * 
 * @param input
 *            the EditText to set the error to
 * @param errorID
 *            the ID of the error in <tt>strings.xml</tt>
 * @param activity
 *            the enclosing activity
 */
private static void setError(EditText input, int errorID, Activity activity) {
    String errorMsg = activity.getResources().getString(errorID);
    if (Build.VERSION.SDK_INT < 11) {
        ForegroundColorSpan fgcspan = new ForegroundColorSpan(Color.BLACK);
        SpannableStringBuilder ssbuilder = new SpannableStringBuilder(errorMsg);
        ssbuilder.setSpan(fgcspan, 0, errorMsg.length(), 0);
        input.setError(ssbuilder);
    } else {
        input.setError(errorMsg);
    }
}

From source file:piuk.blockchain.android.util.WalletUtils.java

public static Editable formatAddress(final String address, final int groupSize, final int lineSize) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();

    final int len = address.length();
    for (int i = 0; i < len; i += groupSize) {
        final int end = i + groupSize;
        final String part = address.substring(i, end < len ? end : len);

        builder.append(part);//from  w w  w.j ava2  s .c o  m
        builder.setSpan(new TypefaceSpan("monospace"), builder.length() - part.length(), builder.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if (end < len) {
            final boolean endOfLine = end % lineSize == 0;
            builder.append(endOfLine ? "\n" : Constants.THIN_SPACE);
        }
    }

    return builder;
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void startSpan(SpannableStringBuilder text, Attributes attributes) {
    String email = attributes.getValue("data-user-email");
    int len = text.length();
    text.setSpan(new Href(email), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void startFont(SpannableStringBuilder text, Attributes attributes) {
    String color = attributes.getValue("", "color");
    String face = attributes.getValue("", "face");

    int len = text.length();
    text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void end(SpannableStringBuilder text, Class kind, Object repl) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);//from   w  ww .j  a  va  2s.c o m

    if (where != len) {
        text.setSpan(repl, where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

}

From source file:com.google.android.apps.iosched.util.UIUtils.java

/**
 * Given a snippet string with matching segments surrounded by curly
 * braces, turn those areas into bold spans, removing the curly braces.
 *///from  w ww  . j  a v a2s  .c om
public static Spannable buildStyledSnippet(String snippet) {
    final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);

    // Walk through string, inserting bold snippet spans
    int startIndex = -1, endIndex = -1, delta = 0;
    while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
        endIndex = snippet.indexOf('}', startIndex);

        // Remove braces from both sides
        builder.delete(startIndex - delta, startIndex - delta + 1);
        builder.delete(endIndex - delta - 1, endIndex - delta);

        // Insert bold style
        builder.setSpan(sBoldSpan, startIndex - delta, endIndex - delta - 1,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        delta += 2;
    }

    return builder;
}