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:com.pranavpandey.smallapp.theme.DynamicTheme.java

/**
 * Highlight the query text within a TextView. Suitable for notifying user about the
 * searched query found in the adapter. TextView should not be empty. Please set your
 * default text first then, highlight the query text by using this function.
 *
 * @param query String to be highlighted.
 * @param textView to set the highlighted text.
 * @param color of the highlighted text.
 *
 * @see {@link android.text.Spannable}./*from w w  w .j a v a2 s  . co m*/
 */
public static void highlightQueryTextColor(@NonNull String query, @NonNull TextView textView,
        @ColorInt int color) {
    final String stringText = textView.getText().toString();
    if (!stringText.isEmpty() && stringText.toLowerCase(Locale.getDefault()).contains(query)) {
        final int startPos = stringText.toLowerCase(Locale.getDefault()).indexOf(query);
        final int endPos = startPos + query.length();

        Spannable spanText = Spannable.Factory.getInstance().newSpannable(stringText);
        spanText.setSpan(new ForegroundColorSpan(color), startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(spanText, TextView.BufferType.SPANNABLE);
    }
}

From source file:com.pranavpandey.smallapp.theme.DynamicTheme.java

/**
 * Highlight the query text within a TextView. Suitable for notifying user about the
 * searched query found in the adapter. TextView should not be empty. Please set your
 * default text first then, highlight the query text by using this function.
 *
 * @param query String to be highlighted.
 * @param textView to set the highlighted text.
 * @param colorId Color id of the highlighted text.
 *
 * @see {@link android.text.Spannable}./*from w ww.j av  a2 s .c om*/
 */
public static void highlightQueryTextColorRes(@NonNull String query, @NonNull TextView textView,
        @ColorRes int colorId) {
    final String stringText = textView.getText().toString();
    if (!stringText.isEmpty() && stringText.toLowerCase(Locale.getDefault()).contains(query)) {
        final int startPos = stringText.toLowerCase(Locale.getDefault()).indexOf(query);
        final int endPos = startPos + query.length();

        Spannable spanText = Spannable.Factory.getInstance().newSpannable(stringText);
        spanText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(textView.getContext(), colorId)),
                startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        textView.setText(spanText, TextView.BufferType.SPANNABLE);
    }
}

From source file:rosmi.acagild.alarmclock.utilities.GeneralUtilities.java

public static void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) textView.getText();
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);//from w ww. j a  v  a2 s  .c o  m
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:cl.monsoon.s1next.binding.TextViewBindingAdapter.java

@BindingAdapter({ "eventBus", "post" })
public static void setCount(TextView textView, EventBus eventBus, Post post) {
    String text = "#" + post.getCount();
    // there is no need to quote #1
    if ("1".equals(post.getCount())) {
        textView.setText(text);// w  w w  .  ja  v a  2  s .  c o  m
    } else {
        Spannable spannable = new SpannableString(text);
        URLSpan urlSpan = new URLSpan(StringUtils.EMPTY) {
            @Override
            public void onClick(@NonNull View widget) {
                eventBus.post(new QuoteEvent(post.getId(), post.getCount()));
            }
        };
        spannable.setSpan(urlSpan, 0, spannable.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(spannable);
    }
}

From source file:dev.drsoran.moloko.util.UIUtils.java

public final static void makeLink(TextView textView, Spannable text, ClickableSpan onClickHandler) {
    if (onClickHandler != null) {
        text.setSpan(onClickHandler, 0, text.length(), 0);
    }/*  w ww  . j a  v  a 2s  .  c  om*/

    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(text, BufferType.SPANNABLE);
}

From source file:com.skalski.websocketsclient.ActivityMain.java

static void appendText(TextView textView, String text, int textColor) {

    int start;//  ww  w. ja v a 2s . c  om
    int end;

    start = textView.getText().length();
    textView.append(text);
    end = textView.getText().length();

    Spannable spannableText = (Spannable) textView.getText();
    spannableText.setSpan(new ForegroundColorSpan(textColor), start, end, 0);
}

From source file:com.microsoft.mimickeralarm.utilities.GeneralUtilities.java

public static void stripUnderlines(TextView textView) {
    Spannable s = (Spannable) textView.getText();
    URLSpan[] spans = s.getSpans(0, s.length(), URLSpan.class);
    for (URLSpan span : spans) {
        int start = s.getSpanStart(span);
        int end = s.getSpanEnd(span);
        s.removeSpan(span);/*w w  w  . jav a  2  s.c  om*/
        span = new URLSpanNoUnderline(span.getURL());
        s.setSpan(span, start, end, 0);
    }
    textView.setText(s);
}

From source file:com.stasbar.knowyourself.Utils.java

/**
 * @param amPmRatio a value between 0 and 1 that is the ratio of the relative size of the
 *                  am/pm string to the time string
 * @return format string for 12 hours mode time
 *//*from w ww.  j a va 2  s . c o  m*/
public static CharSequence get12ModeFormat(float amPmRatio) {
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), "hma");
    if (amPmRatio <= 0) {
        pattern = pattern.replaceAll("a", "").trim();
    }

    // Replace spaces with "Hair Space"
    pattern = pattern.replaceAll(" ", "\u200A");
    // Build a spannable so that the am/pm will be formatted
    int amPmPos = pattern.indexOf('a');
    if (amPmPos == -1) {
        return pattern;
    }

    final Spannable sp = new SpannableString(pattern);
    sp.setSpan(new RelativeSizeSpan(amPmRatio), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new StyleSpan(Typeface.NORMAL), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new TypefaceSpan("sans-serif"), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return sp;
}

From source file:com.androidinspain.deskclock.Utils.java

/**
 * @param amPmRatio      a value between 0 and 1 that is the ratio of the relative size of the
 *                       am/pm string to the time string
 * @param includeSeconds whether or not to include seconds in the time string
 * @return format string for 12 hours mode time, not including seconds
 *///www.ja v a2s  . co  m
public static CharSequence get12ModeFormat(float amPmRatio, boolean includeSeconds) {
    String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), includeSeconds ? "hmsa" : "hma");
    if (amPmRatio <= 0) {
        pattern = pattern.replaceAll("a", "").trim();
    }

    // Replace spaces with "Hair Space"
    pattern = pattern.replaceAll(" ", "\u200A");
    // Build a spannable so that the am/pm will be formatted
    int amPmPos = pattern.indexOf('a');
    if (amPmPos == -1) {
        return pattern;
    }

    final Spannable sp = new SpannableString(pattern);
    sp.setSpan(new RelativeSizeSpan(amPmRatio), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new StyleSpan(Typeface.NORMAL), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    sp.setSpan(new TypefaceSpan("sans-serif"), amPmPos, amPmPos + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return sp;
}

From source file:com.none.tom.simplerssreader.utils.SearchUtils.java

public static void setSearchResultsSpan(final Activity activity, final SearchResults results, final Spannable s,
        final int colorHighlightCurrent, final int colorHighlight, final int colorFont, final int position) {
    final int index = results.columnIndexOf(position);

    int i = 0;//from   w  w  w .j  a v a 2s  .  c  om

    for (final Map.Entry<Integer, Integer> entry : results.getRowIndicesAt(index).entrySet()) {
        final int start = entry.getKey();
        final int end = entry.getValue();

        final BackgroundColorSpan span;

        if (results.isCurrent(index, i)) {
            span = new BackgroundColorSpan(colorHighlightCurrent);
        } else {
            span = new BackgroundColorSpan(colorHighlight);
        }

        s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        if (ThemeUtils.isDarkThemeSelected(activity)) {
            s.setSpan(new ForegroundColorSpan(colorFont), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        i++;
    }
}