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:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void endMultiple(SpannableStringBuilder text, Class kind, Object[] replArray) {
    int len = text.length();
    Object obj = getLast(text, kind);
    int where = text.getSpanStart(obj);

    text.removeSpan(obj);//from ww w. j  a  v a2s.  co  m

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

}

From source file:Main.java

/**
 * Make UI TextView a html link.//w  w  w . j  av a  2  s  .c  o  m
 * 
 * @param context the context
 * @param textView the text view
 * @param html the html containing link info
 */
public static void makeTextViewAHTMLLink(final Context context, TextView textView, String html) {
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = spannableStringBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (final URLSpan urlSpan : urls) {
        int start = spannableStringBuilder.getSpanStart(urlSpan);
        int end = spannableStringBuilder.getSpanEnd(urlSpan);
        int flags = spannableStringBuilder.getSpanFlags(urlSpan);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(urlSpan.getURL()));
                context.startActivity(intent);
            }

            @Override
            public void updateDrawState(TextPaint textPaint) {
                super.updateDrawState(textPaint);
                textPaint.setUnderlineText(false);
            }
        };
        spannableStringBuilder.removeSpan(urlSpan);
        spannableStringBuilder.setSpan(clickable, start, end, flags);
    }
    textView.setText(spannableStringBuilder);
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void start(SpannableStringBuilder text, Object mark) {
    int len = text.length();
    text.setSpan(mark, len, len, Spannable.SPAN_MARK_MARK);
}

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

private static void endSpan(SpannableStringBuilder text) {
    int len = text.length();
    Object obj = getLast(text, Href.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);/*from  www  .j  a  v  a 2s  .c  o  m*/
    if (where != len) {
        Href h = (Href) obj;
        if (h != null && h.mHref != null) {
            if (ZulipApp.get().getEmail().equals(h.mHref)) {
                text.setSpan(new ForegroundColorSpan(userMentionSelfColor), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else {
                text.setSpan(new ProfileSpan(h.mHref, userMentionColor), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }
}

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   w  w  w .j  a v a 2 s . 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:com.zulip.android.util.CustomHtmlToSpannedConverter.java

private static void startImg(SpannableStringBuilder text, Attributes attributes, Html.ImageGetter img) {
    String src = attributes.getValue("", "src");
    Drawable d = null;//from   w  w  w .ja v  a 2 s  .  c  o  m

    if (img != null) {
        d = img.getDrawable(src);
    }

    if (d == null) {
        // don't draw anything
        return;
    }

    int len = text.length();
    text.append("\uFFFC");

    text.setSpan(new ImageSpan(d, src), len, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

From source file:com.freshdigitable.udonroad.CombinedScreenNameTextView.java

public void setNames(User user) {
    final String name = user.getName();
    final String screenName = user.getScreenName();
    if (this.name != null && this.name.equals(name) && this.screenName != null
            && this.screenName.equals(screenName)) {
        return;//from  w w  w  . j  av a  2s.c  om
    }
    final String formatted = name + (TextViewCompat.getMaxLines(this) == 2 ? "\n" : " ") + "@" + screenName;
    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(formatted);
    spannableStringBuilder.setSpan(STYLE_BOLD, 0, name.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    setText(spannableStringBuilder);
    this.name = name;
    this.screenName = screenName;
}

From source file:com.nttec.everychan.ui.presentation.HtmlParser.java

private static void startA(SpannableStringBuilder text, Attributes attributes) {
    String href = attributes.getValue("", "href");

    int len = text.length();
    text.setSpan(new Href(href), len, len, Spannable.SPAN_MARK_MARK);
}

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

private static void startA(SpannableStringBuilder text, Attributes attributes, String baseUri) {
    String href = attributes.getValue("", "href");

    if (href != null && !href.startsWith("http")) {
        String prefix;/*from  w ww.j av a 2 s.c  o m*/
        if (!href.startsWith("/")) {
            prefix = baseUri + "/";
        } else {
            prefix = baseUri;
        }
        href = prefix + href;
    }

    int len = text.length();
    text.setSpan(new Href(href), len, len, Spannable.SPAN_MARK_MARK);
}

From source file:com.madgag.agit.RepoListFragment.java

private void applyToEntireString(SpannableStringBuilder string, CharacterStyle style) {
    string.setSpan(style, 0, string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}