Example usage for android.text Spannable setSpan

List of usage examples for android.text Spannable setSpan

Introduction

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

Prototype

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

Source Link

Document

Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.

Usage

From source file:de.schildbach.wallet.util.MonetarySpannable.java

public static void applyMarkup(final Spannable spannable, @Nullable final Object[] prefixSpans,
        @Nullable final Object[] significantSpans, @Nullable final Object[] insignificantSpans) {
    if (prefixSpans != null)
        for (final Object span : prefixSpans)
            spannable.removeSpan(span);// w  ww.java2 s  .com
    if (significantSpans != null)
        for (final Object span : significantSpans)
            spannable.removeSpan(span);
    if (insignificantSpans != null)
        for (final Object span : insignificantSpans)
            spannable.removeSpan(span);

    final Matcher m = Formats.PATTERN_MONETARY_SPANNABLE.matcher(spannable);
    if (m.find()) {
        int i = 0;

        if (m.group(Formats.PATTERN_GROUP_PREFIX) != null) {
            final int end = m.end(Formats.PATTERN_GROUP_PREFIX);
            if (prefixSpans != null)
                for (final Object span : prefixSpans)
                    spannable.setSpan(span, i, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            i = end;
        }

        if (m.group(Formats.PATTERN_GROUP_SIGNIFICANT) != null) {
            final int end = m.end(Formats.PATTERN_GROUP_SIGNIFICANT);
            if (significantSpans != null)
                for (final Object span : significantSpans)
                    spannable.setSpan(span, i, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            i = end;
        }

        if (m.group(Formats.PATTERN_GROUP_INSIGNIFICANT) != null) {
            final int end = m.end(Formats.PATTERN_GROUP_INSIGNIFICANT);
            if (insignificantSpans != null)
                for (final Object span : insignificantSpans)
                    spannable.setSpan(span, i, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            i = end;
        }
    }
}

From source file:org.onebusaway.android.util.UIUtils.java

public static void setClickableSpan(TextView v, ClickableSpan span) {
    Spannable text = (Spannable) v.getText();
    text.setSpan(span, 0, text.length(), 0);
    v.setMovementMethod(LinkMovementMethod.getInstance());
}

From source file:com.manning.androidhacks.hack010.MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);//from   ww  w  .ja  va  2s . co m

    final TextView textView1 = (TextView) findViewById(R.id.my_text_view_html);
    textView1.setText(Html.fromHtml(getString(R.string.text1)));
    textView1.setMovementMethod(LinkMovementMethod.getInstance());

    final Spannable text2 = new SpannableString(getString(R.string.text2));
    text2.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
    text2.setSpan(new ForegroundColorSpan(Color.BLUE), 5, 9, 0);

    ((TextView) findViewById(R.id.my_text_view_spannable)).setText(text2);
}

From source file:com.icecream.snorlax.module.feature.encounter.EncounterNotification.java

private Spannable getBoldSpannable(String text) {
    Spannable spannable = new SpannableString(text);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, spannable.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
    return spannable;
}

From source file:Main.java

/**
 * replace existing spannable with smiles
 * @param context/*from w  w w .j ava 2s .  c o m*/
 * @param spannable
 * @return
 */
public static boolean addSmiles(Context context, Spannable spannable) {
    boolean hasChanges = false;
    for (Entry<Pattern, Object> entry : emoticons.entrySet()) {
        Matcher matcher = entry.getKey().matcher(spannable);
        while (matcher.find()) {
            boolean set = true;
            for (ImageSpan span : spannable.getSpans(matcher.start(), matcher.end(), ImageSpan.class))
                if (spannable.getSpanStart(span) >= matcher.start()
                        && spannable.getSpanEnd(span) <= matcher.end())
                    spannable.removeSpan(span);
                else {
                    set = false;
                    break;
                }
            if (set) {
                hasChanges = true;
                Object value = entry.getValue();
                if (value instanceof String && !((String) value).startsWith("http")) {
                    File file = new File((String) value);
                    if (!file.exists() || file.isDirectory()) {
                        return false;
                    }
                    spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)), matcher.start(),
                            matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannable.setSpan(new ImageSpan(context, (Integer) value), matcher.start(), matcher.end(),
                            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
    }

    return hasChanges;
}

From source file:com.example.cassi.hal.adapter.CardPresenter.java

private Spannable getSeedAndLeech(T411TorrentItem item) {
    String text = "Seed : " + String.valueOf(item.getSeeders()) + " Leech : "
            + String.valueOf(item.getLeechers());
    Spannable spannable = new SpannableString(text);

    spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.seedColor)),
            text.indexOf(String.valueOf(item.getSeeders())),
            text.indexOf(String.valueOf(item.getSeeders())) + String.valueOf(item.getSeeders()).length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.leechColor)),
            text.lastIndexOf(String.valueOf(item.getLeechers())),
            text.lastIndexOf(String.valueOf(item.getLeechers())) + String.valueOf(item.getLeechers()).length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return spannable;
}

From source file:io.github.marktony.espresso.mvp.companydetails.CompanyDetailFragment.java

@Override
public void setCompanyWebsite(String website) {
    this.website = website;
    String ws = getString(R.string.official_website) + "\n" + website;
    Spannable spannable = new SpannableStringBuilder(ws);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, ws.length() - website.length() - 1,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new URLSpan(website), ws.length() - website.length(), ws.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textViewWebsite.setText(spannable);//from  www. j  av a 2 s  .  c om
}

From source file:io.github.marktony.espresso.mvp.companydetails.CompanyDetailFragment.java

@Override
public void setCompanyName(String name) {
    String companyName = getString(R.string.company_name) + "\n" + name;
    Spannable spannable = new SpannableStringBuilder(companyName);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, companyName.length() - name.length() - 1,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new StyleSpan(Typeface.NORMAL), companyName.length() - name.length(),
            companyName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textViewCompanyName.setText(spannable);
}

From source file:io.github.marktony.espresso.mvp.companydetails.CompanyDetailFragment.java

@Override
public void setCompanyTel(String tel) {
    this.tel = tel;
    String companyTel = getString(R.string.phone_number) + "\n" + tel;
    Spannable spannable = new SpannableStringBuilder(companyTel);
    spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, companyTel.length() - tel.length() - 1,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannable.setSpan(new URLSpan(tel), companyTel.length() - tel.length(), companyTel.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textViewTel.setText(spannable);//from w  w  w  . j ava  2 s .  co m
}

From source file:io.relayr.tellmewhen.gcm.GcmIntentService.java

private InboxStyle prepareBigNotificationDetails(Spannable spanTitle) {
    NotificationCompat.InboxStyle result = new InboxStyle();
    result.setBigContentTitle(spanTitle);

    for (Map.Entry<Pair<SensorType, String>, Float> entry : pushedRules.entrySet()) {
        SensorType sensorType = entry.getKey().first;

        String ruleName = entry.getKey().second;
        if (ruleName.length() <= 20) {
            int empty = 20 - ruleName.length();
            for (int i = 0; i < empty; i++) {
                ruleName += " ";
            }//from w ww.  ja v  a  2s .c o m
        } else {
            ruleName = entry.getKey().second.substring(0, 20);
        }

        String notificationText = getString(R.string.push_notification_value) + " "
                + SensorUtil.scaleToUiData(sensorType, entry.getValue()) + sensorType.getUnit();

        String all = ruleName + " (" + notificationText + ")";

        Spannable spanNotif = new SpannableString(all);
        spanNotif.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, ruleName.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        result.addLine(spanNotif);
    }

    result.setSummaryText(getString(R.string.push_notification_summary));

    return result;
}