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.pocketsoap.admin.UserDetailFragment.java

private void bindUi() {
    // header section
    setText(R.id.detail_name, user.Name);
    setText(R.id.detail_username, user.Username);
    setText(R.id.detail_title, user.Title);

    // contact section
    setText(R.id.contact_email, user.Email);
    setText(R.id.contact_phone, user.Phone);
    setText(R.id.contact_mobile, user.MobilePhone);

    // no auto link for SMS, so we need to build our own URLSpan for it.
    if (user.MobilePhone != null && user.MobilePhone.length() > 0) {
        SpannableStringBuilder b = new SpannableStringBuilder(user.MobilePhone);
        b.setSpan(new URLSpan("smsto:" + user.MobilePhone), 0, user.MobilePhone.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        setText(R.id.contact_mobile_text, b).setMovementMethod(LinkMovementMethod.getInstance());
    } else {/*from  w  w w .  j a  va 2 s  .  c  o  m*/
        setText(R.id.contact_mobile_text, "");
    }

    //action section
    isActive.setChecked(user.IsActive);
    isActive.setOnClickListener(new ToggleActive());

    // user photo
    // the default person image is https://blah/.../005/T but we don't want to bother fetching that, we'll just use our local default instead.
    if (user.SmallPhotoUrl != null && user.SmallPhotoUrl.length() > 0
            && !user.SmallPhotoUrl.endsWith("/005/T")) {
        PhotoLoaderTask photoLoader = new PhotoLoaderTask(getActivityHelper());
        photoLoader.execute(user.SmallPhotoUrl);
    } else {
        this.userPhoto.setImageResource(R.drawable.ic_contact_picture);
    }
}

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

private static void endAibspoiler(SpannableStringBuilder text, ThemeColors colors, boolean openSpoilers) {
    int len = text.length();
    Object obj = getLast(text, Aibspoiler.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);// w w w . j ava 2  s  . c o  m

    if (where != len && colors != null) {
        if (openSpoilers) {
            text.setSpan(new ForegroundColorSpan(colors.spoilerForeground), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            text.setSpan(new BackgroundColorSpan(colors.spoilerBackground), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        } else {
            text.setSpan(new SpoilerSpan(colors.spoilerForeground, colors.spoilerBackground), where, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

static CharSequence applyWarningTextColor(final Context context, final CharSequence text) {
    if (text == null) {
        return null;
    }/*from ww w .j  a v  a2  s.  c o  m*/
    final SpannableStringBuilder spanBuilder = new SpannableStringBuilder();
    spanBuilder.append(text);
    spanBuilder.setSpan(
            new ForegroundColorSpan(context.getResources().getColor(R.color.notification_warning_color)), 0,
            text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return spanBuilder;
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

/**
 * buildBoldedMessage - build a formatted message where the title is bold, there's a
 * separator, then the message./*from   www .  j  a  v a2 s  .co m*/
 */
private static CharSequence buildBoldedMessage(final String title, final CharSequence message,
        final Uri attachmentUri, final String attachmentType, final int separatorId) {
    final Context context = Factory.get().getApplicationContext();
    final SpannableStringBuilder spanBuilder = new SpannableStringBuilder();

    // Boldify the title (which is the sender's name)
    if (!TextUtils.isEmpty(title)) {
        spanBuilder.append(title);
        spanBuilder.setSpan(new StyleSpan(Typeface.BOLD), 0, title.length(),
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (!TextUtils.isEmpty(message)) {
        if (spanBuilder.length() > 0) {
            spanBuilder.append(context.getString(separatorId));
        }
        spanBuilder.append(message);
    }
    if (attachmentUri != null) {
        if (spanBuilder.length() > 0) {
            final String separator = context.getString(R.string.notification_separator);
            spanBuilder.append(separator);
        }
        spanBuilder.append(formatAttachmentTag(null, attachmentType));
    }
    return spanBuilder;
}

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

/**
 * ? ? SpoilerSpan  ForegroundColorSpan  ?? ?   ??  ?
 *//*from  w ww  .j  a va2s  . c  o m*/
private static void fixSpoilerSpans(SpannableStringBuilder builder, ThemeColors themeColors) {
    SpoilerSpan[] spoilers = builder.getSpans(0, builder.length(), SpoilerSpan.class);
    for (SpoilerSpan span : spoilers) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        builder.removeSpan(span);
        builder.setSpan(span, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    ClickableURLSpan[] urls = builder.getSpans(0, builder.length(), ClickableURLSpan.class);
    for (ClickableURLSpan span : urls) {
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        builder.setSpan(new ForegroundColorSpan(themeColors.urlLinkForeground), start, end,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

From source file:net.zionsoft.obadiah.ui.adapters.TranslationListAdapter.java

public void setTranslations(@Nullable List<TranslationInfo> downloaded,
        @Nullable List<TranslationInfo> available) {
    mSectionHeaders.clear();//from ww  w.  j a  v a2s  .  c  o m
    mTranslations.clear();
    mCount = 0;
    if (downloaded != null && downloaded.size() > 0) {
        final List<TranslationInfoHolder> translations = new ArrayList<TranslationInfoHolder>(
                downloaded.size());
        for (TranslationInfo translationInfo : downloaded) {
            final SpannableStringBuilder text = new SpannableStringBuilder(translationInfo.name);
            text.setSpan(mMediumSizeSpan, 0, translationInfo.name.length(), 0);

            translations.add(new TranslationInfoHolder(translationInfo, text, true));
        }
        mSectionHeaders.add(mDownloadedTranslationsTitle);
        mTranslations.add(translations);
        mCount = downloaded.size() + 1;
    }

    if (available != null) {
        for (TranslationInfo translationInfo : available) {
            final String language = new Locale(translationInfo.language.split("_")[0]).getDisplayLanguage();
            int index = 0;
            List<TranslationInfoHolder> translations = null;
            for (String sectionHeader : mSectionHeaders) {
                if (sectionHeader.equals(language)) {
                    translations = mTranslations.get(index);
                    break;
                }
                ++index;
            }
            if (translations == null) {
                translations = new ArrayList<TranslationInfoHolder>();
                mSectionHeaders.add(language);
                mTranslations.add(translations);
                ++mCount;
            }

            final SpannableStringBuilder text = new SpannableStringBuilder(
                    mResources.getString(R.string.text_available_translation_info, translationInfo.name,
                            translationInfo.size / 1024));
            text.setSpan(mMediumSizeSpan, 0, translationInfo.name.length(), 0);
            text.setSpan(mSmallSizeSpan, translationInfo.name.length(), text.length(), 0);

            translations.add(new TranslationInfoHolder(translationInfo, text, false));
            ++mCount;
        }
    }
}

From source file:com.android.tv.dvr.ui.DvrUiHelper.java

@NonNull
public static CharSequence getStyledTitleWithEpisodeNumber(Context context, String title, String seasonNumber,
        String episodeNumber, int episodeNumberStyleResId) {
    if (TextUtils.isEmpty(title)) {
        return "";
    }/*from   ww  w  .  j  a  v  a  2s .  com*/
    SpannableStringBuilder builder;
    if (TextUtils.isEmpty(seasonNumber) || seasonNumber.equals("0")) {
        builder = TextUtils.isEmpty(episodeNumber) ? new SpannableStringBuilder(title)
                : new SpannableStringBuilder(Html.fromHtml(context.getString(
                        R.string.program_title_with_episode_number_no_season, title, episodeNumber)));
    } else {
        builder = new SpannableStringBuilder(Html.fromHtml(context
                .getString(R.string.program_title_with_episode_number, title, seasonNumber, episodeNumber)));
    }
    Object[] spans = builder.getSpans(0, builder.length(), Object.class);
    if (spans.length > 0) {
        if (episodeNumberStyleResId != 0) {
            builder.setSpan(new TextAppearanceSpan(context, episodeNumberStyleResId),
                    builder.getSpanStart(spans[0]), builder.getSpanEnd(spans[0]),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        builder.removeSpan(spans[0]);
    }
    return new SpannableString(builder);
}

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

public static SpannableStringBuilder createMessageInfo(Context context, Conversation conv,
        final boolean resourceCachingRequired) {
    SpannableStringBuilder messageInfo = new SpannableStringBuilder();

    try {//  w  w  w .  j ava2 s.  c  om
        ConversationInfo conversationInfo = conv.conversationInfo;
        int sendingStatus = conv.sendingState;
        boolean hasSenders = false;
        // This covers the case where the sender is "me" and this is a draft
        // message, which means this will only run once most of the time.
        for (ParticipantInfo p : conversationInfo.participantInfos) {
            if (!TextUtils.isEmpty(p.name)) {
                hasSenders = true;
                break;
            }
        }
        getSenderResources(context, resourceCachingRequired);
        int count = conversationInfo.messageCount;
        int draftCount = conversationInfo.draftCount;
        if (count > 1) {
            messageInfo.append(count + "");
        }
        messageInfo.setSpan(
                CharacterStyle.wrap(conv.read ? sMessageInfoReadStyleSpan : sMessageInfoUnreadStyleSpan), 0,
                messageInfo.length(), 0);
        if (draftCount > 0) {
            // If we are showing a message count or any draft text and there
            // is at least 1 sender, prepend the sending state text with a
            // comma.
            if (hasSenders || count > 1) {
                messageInfo.append(sSendersSplitToken);
            }
            SpannableStringBuilder draftString = new SpannableStringBuilder();
            if (draftCount == 1) {
                draftString.append(sDraftSingularString);
            } else {
                draftString.append(sDraftPluralString)
                        .append(String.format(sDraftCountFormatString, draftCount));
            }
            draftString.setSpan(CharacterStyle.wrap(sDraftsStyleSpan), 0, draftString.length(),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            messageInfo.append(draftString);
        }
        // TS: chao.zhang 2015-09-29 EMAIL FEATURE-585337 MOD_S
        //NOTE:currently,we DONOT show the status in senderView, with our ERGO,show the status on the side if Subject.diable it!!!
        //boolean showState = sendingStatus == UIProvider.ConversationSendingState.SENDING ||
        //        sendingStatus == UIProvider.ConversationSendingState.RETRYING ||
        //       sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR;
        boolean showState = false;
        // TS: chao.zhang 2015-09-29 EMAIL FEATURE-585337 MOD_E
        if (showState) {
            // If we are showing a message count or any draft text, prepend
            // the sending state text with a comma.
            if (count > 1 || draftCount > 0) {
                messageInfo.append(sSendersSplitToken);
            }

            SpannableStringBuilder stateSpan = new SpannableStringBuilder();

            if (sendingStatus == UIProvider.ConversationSendingState.SENDING) {
                stateSpan.append(sSendingString);
                stateSpan.setSpan(sSendingStyleSpan, 0, stateSpan.length(), 0);
            } else if (sendingStatus == UIProvider.ConversationSendingState.RETRYING) {
                stateSpan.append(sQueuedString);
                stateSpan.setSpan(sQueuedStyleSpan, 0, stateSpan.length(), 0);
            } else if (sendingStatus == UIProvider.ConversationSendingState.SEND_ERROR) {
                stateSpan.append(sFailedString);
                stateSpan.setSpan(sFailedStyleSpan, 0, stateSpan.length(), 0);
            }
            messageInfo.append(stateSpan);
        }

        // Prepend a space if we are showing other message info text.
        if (count > 1 || (draftCount > 0 && hasSenders) || showState) {
            messageInfo.insert(0, sMessageCountSpacerString);
        }
    } finally {
        if (!resourceCachingRequired) {
            clearResourceCache();
        }
    }

    return messageInfo;
}

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

private static void endBlockquote(SpannableStringBuilder text, ThemeColors colors) {
    int len = text.length();
    Object obj = getLast(text, Blockquote.class);
    int where = text.getSpanStart(obj);
    text.removeSpan(obj);/*from  w  w  w  .j a va2 s. c o m*/

    if (where != len) {
        Blockquote b = (Blockquote) obj;
        if (b.mIsUnkfunc) {
            if (colors != null) {
                text.setSpan(new ForegroundColorSpan(colors.quoteForeground), where, len,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else {
            text.setSpan(new QuoteSpan(), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}

From source file:ca.rmen.android.poetassistant.main.dictionaries.ResultListFragment.java

private void updateUi() {
    Log.d(TAG, mTab + ": updateUi() called with: " + "");
    if (mAdapter.getItemCount() > 0 || !TextUtils.isEmpty(mHeaderFragment.getHeader())) {
        mHeaderFragment.show();/* w w w .  ja v  a2  s . co m*/
    } else {
        mHeaderFragment.hide();
    }

    String query = mData == null ? null : mData.matchedWord;
    mHeaderFragment.setHeader(query);
    // If we have an empty list because the user didn't enter any search term,
    // we'll show a text to tell them to search.
    if (TextUtils.isEmpty(query)) {
        String emptySearch = getString(R.string.empty_list_without_query);
        ImageSpan imageSpan = VectorCompat.createVectorImageSpan(getActivity(),
                R.drawable.ic_action_search_dark);
        SpannableStringBuilder ssb = new SpannableStringBuilder(emptySearch);
        int iconIndex = emptySearch.indexOf("%s");
        ssb.setSpan(imageSpan, iconIndex, iconIndex + 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mBinding.empty.setText(ssb, TextView.BufferType.SPANNABLE);
    }
    // If the user entered a query and there are no matches, show the normal "no results" text.
    else {
        String noResults = ResultListFactory.getEmptyListText(getContext(), mTab, query);
        mBinding.empty.setText(noResults);
    }
    if (mData == null || mData.data.isEmpty()) {
        mBinding.empty.setVisibility(View.VISIBLE);
        mBinding.recyclerView.setVisibility(View.GONE);
    } else {
        mBinding.empty.setVisibility(View.GONE);
        mBinding.recyclerView.setVisibility(View.VISIBLE);
    }

    if (mData != null)
        mHeaderFragment.setFavorite(mData.isFavorite);

    getActivity().supportInvalidateOptionsMenu();
}