List of usage examples for android.text SpannableStringBuilder valueOf
public static SpannableStringBuilder valueOf(CharSequence source)
From source file:org.getlantern.firetweet.fragment.support.UserFragment.java
private void updateTitleColor() { final int[] location = new int[2]; mNameView.getLocationOnScreen(location); final float nameShowingRatio = (mHeaderDrawerLayout.getPaddingTop() - location[1]) / (float) mNameView.getHeight(); final int textAlpha = Math.round(0xFF * MathUtils.clamp(nameShowingRatio, 0, 1)); final FragmentActivity activity = getActivity(); final SpannableStringBuilder spannedTitle; final CharSequence title = activity.getTitle(); if (title instanceof SpannableStringBuilder) { spannedTitle = (SpannableStringBuilder) title; } else {/*from w ww . j a v a 2 s. c om*/ spannedTitle = SpannableStringBuilder.valueOf(title); } final TextAlphaSpan[] spans = spannedTitle.getSpans(0, spannedTitle.length(), TextAlphaSpan.class); if (spans.length > 0) { spans[0].setAlpha(textAlpha); } else { spannedTitle.setSpan(new TextAlphaSpan(textAlpha), 0, spannedTitle.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } activity.setTitle(spannedTitle); }
From source file:de.vanita5.twittnuker.util.Utils.java
public static CharSequence getKeywordHighlightedText(final CharSequence orig, final CharacterStyle style, final String... keywords) { if (keywords == null || keywords.length == 0 || orig == null) return orig; final SpannableStringBuilder sb = SpannableStringBuilder.valueOf(orig); final StringBuilder patternBuilder = new StringBuilder(); for (int i = 0, j = keywords.length; i < j; i++) { if (i != 0) { patternBuilder.append('|'); }/* ww w . jav a 2s .c o m*/ patternBuilder.append(Pattern.quote(keywords[i])); } final Matcher m = Pattern.compile(patternBuilder.toString(), Pattern.CASE_INSENSITIVE).matcher(orig); while (m.find()) { sb.setSpan(style, m.start(), m.end(), SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); } return sb; }
From source file:kr.wdream.ui.ChatActivity.java
private void applyDraftMaybe(boolean canClear) { if (chatActivityEnterView == null) { return;/* w w w .j a va 2 s .co m*/ } TLRPC.DraftMessage draftMessage = DraftQuery.getDraft(dialog_id); TLRPC.Message draftReplyMessage = draftMessage != null && draftMessage.reply_to_msg_id != 0 ? DraftQuery.getDraftMessage(dialog_id) : null; if (chatActivityEnterView.getFieldText() == null) { if (draftMessage != null) { chatActivityEnterView.setWebPage(null, !draftMessage.no_webpage); CharSequence message; if (!draftMessage.entities.isEmpty()) { SpannableStringBuilder stringBuilder = SpannableStringBuilder.valueOf(draftMessage.message); MessagesQuery.sortEntities(draftMessage.entities); int addToOffset = 0; for (int a = 0; a < draftMessage.entities.size(); a++) { TLRPC.MessageEntity entity = draftMessage.entities.get(a); if (entity instanceof TLRPC.TL_inputMessageEntityMentionName || entity instanceof TLRPC.TL_messageEntityMentionName) { int user_id; if (entity instanceof TLRPC.TL_inputMessageEntityMentionName) { user_id = ((TLRPC.TL_inputMessageEntityMentionName) entity).user_id.user_id; } else { user_id = ((TLRPC.TL_messageEntityMentionName) entity).user_id; } if (entity.offset + addToOffset + entity.length < stringBuilder.length() && stringBuilder.charAt(entity.offset + addToOffset + entity.length) == ' ') { entity.length++; } stringBuilder.setSpan(new URLSpanUserMention("" + user_id), entity.offset + addToOffset, entity.offset + addToOffset + entity.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else if (entity instanceof TLRPC.TL_messageEntityCode) { stringBuilder.insert(entity.offset + entity.length + addToOffset, "`"); stringBuilder.insert(entity.offset + addToOffset, "`"); addToOffset += 2; } else if (entity instanceof TLRPC.TL_messageEntityPre) { stringBuilder.insert(entity.offset + entity.length + addToOffset, "```"); stringBuilder.insert(entity.offset + addToOffset, "```"); addToOffset += 6; } } message = stringBuilder; } else { message = draftMessage.message; } chatActivityEnterView.setFieldText(message); if (getArguments().getBoolean("hasUrl", false)) { chatActivityEnterView.setSelection(draftMessage.message.indexOf('\n') + 1); AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (chatActivityEnterView != null) { chatActivityEnterView.setFieldFocused(true); chatActivityEnterView.openKeyboard(); } } }, 700); } } } else if (canClear && draftMessage == null) { chatActivityEnterView.setFieldText(""); showReplyPanel(false, null, null, null, false, true); } if (replyingMessageObject == null && draftReplyMessage != null) { replyingMessageObject = new MessageObject(draftReplyMessage, MessagesController.getInstance().getUsers(), false); showReplyPanel(true, replyingMessageObject, null, null, false, false); } }