Java Code Examples for android.text.Editable#setSpan()
The following examples show how to use
android.text.Editable#setSpan() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShaderEditor.java From ShaderEditor with MIT License | 6 votes |
private void convertTabs(Editable e, int start, int count) { if (tabWidth < 1) { return; } String s = e.toString(); for (int stop = start + count; (start = s.indexOf("\t", start)) > -1 && start < stop; ++start) { e.setSpan( new TabWidthSpan(tabWidth), start, start + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } }
Example 2
Source File: MoonUtil.java From RxZhihuDaily with MIT License | 6 votes |
public static void replaceEmoticons(Context context, Editable editable, int start, int count) { if (count <= 0 || editable.length() < start + count) return; CharSequence s = editable.subSequence(start, start + count); Matcher matcher = EmoUtil.getPattern().matcher(s); while (matcher.find()) { int from = start + matcher.start(); int to = start + matcher.end(); String emot = editable.subSequence(from, to).toString(); Drawable d = getEmotDrawable(context, emot, DEF_SCALE); if (d != null) { ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
Example 3
Source File: InputPanel.java From Android with MIT License | 6 votes |
public void replaceEmoticons(Editable editable, int start, int count) { if (count <= 0 || editable.length() < start + count) return; CharSequence s = editable.subSequence(start, start + count); Matcher matcher = Pattern.compile("\\[[^\\[]+\\]").matcher(s); while (matcher.find()) { int from = start + matcher.start(); int to = start + matcher.end(); String emot = editable.subSequence(from, to).toString(); emot = emot.substring(1, emot.length() - 1) + ".png"; String key = StickerCategory.emojiMaps.get(emot); if (!TextUtils.isEmpty(key)) { emot = key; } Drawable d = ResourceUtil.getEmotDrawable(emot); if (d != null) { ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM); editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
Example 4
Source File: MentionEditText.java From MentionEditText with Apache License 2.0 | 5 votes |
private void colorMentionString() { //reset state mIsSelected = false; if (mRangeArrayList != null) { mRangeArrayList.clear(); } Editable spannableText = getText(); if (spannableText == null || TextUtils.isEmpty(spannableText.toString())) { return; } //remove previous spans ForegroundColorSpan[] oldSpans = spannableText.getSpans(0, spannableText.length(), ForegroundColorSpan.class); for (ForegroundColorSpan oldSpan : oldSpans) { spannableText.removeSpan(oldSpan); } //find mention string and color it String text = spannableText.toString(); for (Map.Entry<String, Pattern> entry : mPatternMap.entrySet()) { int lastMentionIndex = -1; Matcher matcher = entry.getValue().matcher(text); while (matcher.find()) { String mentionText = matcher.group(); int start; if (lastMentionIndex != -1) { start = text.indexOf(mentionText, lastMentionIndex); } else { start = text.indexOf(mentionText); } int end = start + mentionText.length(); spannableText.setSpan(new ForegroundColorSpan(mMentionTextColor), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); lastMentionIndex = end; //record all mention-string's position mRangeArrayList.add(new Range(start, end)); } } }
Example 5
Source File: LocationInformation.java From trekarta with GNU General Public License v3.0 | 5 votes |
@Override public void afterTextChanged(Editable s) { if (s.length() == 0) { mTextInputDialog.setDescription(""); return; } try { JosmCoordinatesParser.Result result = JosmCoordinatesParser.parseWithResult(s.toString()); s.setSpan( new ForegroundColorSpan(mColorDarkBlue), 0, result.offset, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); s.setSpan( new ForegroundColorSpan(mColorTextPrimary), result.offset, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mTextInputDialog.setDescription(StringFormatter.coordinates(" ", result.coordinates.getLatitude(), result.coordinates.getLongitude())); } catch (IllegalArgumentException e) { s.setSpan( new ForegroundColorSpan(mColorRed), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mTextInputDialog.setDescription(""); } }
Example 6
Source File: CustomHtmlTagHandler.java From SteamGifts with MIT License | 5 votes |
private void processSpoiler(boolean opening, Editable output) { int len = output.length(); if (opening) { output.setSpan(new Spoiler(), len, len, Spannable.SPAN_MARK_MARK); } else { Object obj = getLast(output, Spoiler.class); int where = output.getSpanStart(obj); output.removeSpan(obj); if (where != len) { char[] str = new char[len - where]; output.getChars(where, len, str, 0); final String text = String.valueOf(str); output.setSpan(new ClickableSpan() { @Override public void onClick(View widget) { Dialog dialog = new Dialog(widget.getContext()); dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(R.layout.spoiler_dialog); ((TextView) dialog.findViewById(R.id.text)).setText(text); dialog.show(); } }, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); output.setSpan(new ForegroundColorSpan(0xff666666), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); output.setSpan(new BackgroundColorSpan(0xff666666), where, len, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
Example 7
Source File: MentionsEditText.java From Spyglass with Apache License 2.0 | 5 votes |
/** * Resets the given {@link MentionSpan} in the editor, forcing it to redraw with its latest drawable state. * * @param span the {@link MentionSpan} to update */ public void updateSpan(@NonNull MentionSpan span) { mBlockCompletion = true; Editable text = getText(); int start = text.getSpanStart(span); int end = text.getSpanEnd(span); if (start >= 0 && end > start && end <= text.length()) { text.removeSpan(span); text.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } mBlockCompletion = false; }
Example 8
Source File: TokenCompleteTextView.java From TokenAutoComplete with Apache License 2.0 | 5 votes |
@Override protected void replaceText(CharSequence ignore) { clearComposingText(); // Don't build a token for an empty String if (selectedObject == null || selectedObject.toString().equals("")) return; TokenImageSpan tokenSpan = buildSpanForObject(selectedObject); Editable editable = getText(); Range candidateRange = getCurrentCandidateTokenRange(); String original = TextUtils.substring(editable, candidateRange.start, candidateRange.end); //Keep track of replacements for a bug workaround if (original.length() > 0) { lastCompletionText = original; } if (editable != null) { internalEditInProgress = true; if (tokenSpan == null) { editable.replace(candidateRange.start, candidateRange.end, ""); } else if (shouldIgnoreToken(tokenSpan.getToken())) { editable.replace(candidateRange.start, candidateRange.end, ""); if (listener != null) { listener.onTokenIgnored(tokenSpan.getToken()); } } else { SpannableStringBuilder ssb = new SpannableStringBuilder(tokenizer.wrapTokenValue(tokenToString(tokenSpan.token))); editable.replace(candidateRange.start, candidateRange.end, ssb); editable.setSpan(tokenSpan, candidateRange.start, candidateRange.start + ssb.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); editable.insert(candidateRange.start + ssb.length(), " "); } internalEditInProgress = false; } }
Example 9
Source File: TextShow.java From WeCenterMobile-Android with GNU General Public License v2.0 | 5 votes |
@Override public void handleTag(boolean arg0, String arg1, Editable arg2, XMLReader arg3) { // TODO Auto-generated method stub if (arg1.equalsIgnoreCase("img")) { arg2.setSpan(new GameSpan(tag), arg2.length()-1, arg2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); tag++; } }
Example 10
Source File: CardEditText.java From android-card-form with MIT License | 5 votes |
private void addSpans(Editable editable, int[] spaceIndices) { final int length = editable.length(); for (int index : spaceIndices) { if (index <= length) { editable.setSpan(new SpaceSpan(), index - 1, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } } }
Example 11
Source File: RTManager.java From memoir with Apache License 2.0 | 5 votes |
private void insertImage(final RTEditText editor, final RTImage image) { if (image != null && editor != null) { Selection selection = new Selection(editor); Editable str = editor.getText(); // Unicode Character 'OBJECT REPLACEMENT CHARACTER' (U+FFFC) // see http://www.fileformat.info/info/unicode/char/fffc/index.htm str.insert(selection.start(), "\uFFFC"); try { // now add the actual image and inform the RTOperationManager about the operation Spannable oldSpannable = editor.cloneSpannable(); ImageSpan imageSpan = new ImageSpan(image, false); str.setSpan(imageSpan, selection.start(), selection.end() + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); int selStartAfter = editor.getSelectionStart(); int selEndAfter = editor.getSelectionEnd(); editor.onAddMedia(image); Spannable newSpannable = editor.cloneSpannable(); mOPManager.executed(editor, new RTOperationManager.TextChangeOperation(oldSpannable, newSpannable, selection.start(), selection.end(), selStartAfter, selEndAfter)); } catch (OutOfMemoryError e) { str.delete(selection.start(), selection.end() + 1); mRTApi.makeText(R.string.rte_add_image_error, Toast.LENGTH_LONG).show(); } } }
Example 12
Source File: FormattedEditText.java From FormatEditText with MIT License | 5 votes |
private void formatDefined(Editable editable, int start, boolean deletion) { start = clearPlaceholders(editable, start); int selectionIndex = -1; int indexInText = start; final int maxPos = mHolders[mHolders.length - 1].index; while (indexInText < maxPos) { if (indexInText >= editable.length()) { break; } char placeholder = findPlaceholder(indexInText); if (placeholder != 0) { editable.insert(indexInText, String.valueOf(placeholder)); editable.setSpan( new PlaceholderSpan(), indexInText, indexInText + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); indexInText += 1; if (selectionIndex == -1) { if (indexInText == start + 1) { selectionIndex = indexInText; } } else if (indexInText == selectionIndex + 1) { selectionIndex = indexInText; } } else { indexInText += 1; } } if (deletion && start == 0 && selectionIndex != -1) { editable.setSpan( SELECTION_SPAN, selectionIndex, selectionIndex, Spanned.SPAN_MARK_MARK); } }
Example 13
Source File: MnemonicActivity.java From green_android with GNU General Public License v3.0 | 5 votes |
private boolean markInvalidWord(final Editable e) { for (final StrikethroughSpan s : e.getSpans(0, e.length(), StrikethroughSpan.class)) e.removeSpan(s); final String word = e.toString(); if (isHexSeed(word)) return true; final int end = word.length(); if (!MnemonicHelper.isPrefix(word)) e.setSpan(new StrikethroughSpan(), 0, end, 0); return !MnemonicHelper.mWords.contains(word); }
Example 14
Source File: ListTagHandler.java From zulip-android with Apache License 2.0 | 5 votes |
/** * Opens a new list item. * * @param text */ public void openItem(final Editable text) { if (text.length() > 0 && text.charAt(text.length() - 1) != '\n') { text.append("\n"); } final int len = text.length(); text.setSpan(this, len, len, Spanned.SPAN_MARK_MARK); }
Example 15
Source File: ExpirationDateEditText.java From android-card-form with MIT License | 5 votes |
private void addDateSlash(Editable editable) { final int index = 2; final int length = editable.length(); if (index <= length) { editable.setSpan(new SlashSpan(), index - 1, index, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } }
Example 16
Source File: SpanFormatter.java From nfcard with GNU General Public License v3.0 | 4 votes |
private static void setSpan(Editable out, int pos, Class<?> kind) { Object span = getLastMarkSpan(out, kind); out.setSpan(span, out.getSpanStart(span), pos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); }
Example 17
Source File: RecipientEditTextView.java From talk-android with MIT License | 4 votes |
/** * Create a chip that represents just the email address of a recipient. At some later * point, this chip will be attached to a real contact entry, if one exists. */ // VisibleForTesting void createReplacementChip(int tokenStart, int tokenEnd, Editable editable, boolean visible) { if (alreadyHasChip(tokenStart, tokenEnd)) { // There is already a chip present at this location. // Don't recreate it. return; } String token = editable.toString().substring(tokenStart, tokenEnd); final String trimmedToken = token.trim(); int commitCharIndex = trimmedToken.lastIndexOf(COMMIT_CHAR_COMMA); if (commitCharIndex != -1 && commitCharIndex == trimmedToken.length() - 1) { token = trimmedToken.substring(0, trimmedToken.length() - 1); } RecipientEntry entry = createTokenizedEntry(token); if (entry != null) { DrawableRecipientChip chip = null; try { if (!mNoChips) { /* * leave space for the contact icon if this is not just an * email address */ boolean leaveSpace = TextUtils.isEmpty(entry.getDisplayName()) || TextUtils.equals(entry.getDisplayName(), entry.getDestination()); chip = visible ? constructChipSpan(entry, false, leaveSpace) : new InvisibleRecipientChip(entry); } } catch (NullPointerException e) { Log.e(TAG, e.getMessage(), e); } editable.setSpan(chip, tokenStart, tokenEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); // Add this chip to the list of entries "to replace" if (chip != null) { if (mTemporaryRecipients == null) { mTemporaryRecipients = new ArrayList<DrawableRecipientChip>(); } chip.setOriginalText(token); mTemporaryRecipients.add(chip); } } }
Example 18
Source File: SpanFormatter.java From nfcard with GNU General Public License v3.0 | 4 votes |
private void markActionSpan(Editable out, int pos, String tag, int colorId) { int color = ThisApplication.getColorResource(colorId); out.setSpan(new ActionSpan(tag, handler, color), pos, pos, Spannable.SPAN_MARK_MARK); }
Example 19
Source File: SpanFormatter.java From NFCard with GNU General Public License v3.0 | 4 votes |
private static void markFontSpan(Editable out, int pos, int colorId, int sizeId, Typeface face) { int color = ThisApplication.getColorResource(colorId); float size = ThisApplication.getDimensionResourcePixelSize(sizeId); FontSpan span = new FontSpan(color, size, face); out.setSpan(span, pos, pos, Spannable.SPAN_MARK_MARK); }
Example 20
Source File: HtmlTagHandler.java From android-discourse with Apache License 2.0 | 4 votes |
@Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { if (tag.equalsIgnoreCase("ul")) { if (opening) { lists.push(tag); } else { lists.pop(); } } else if (tag.equalsIgnoreCase("ol")) { if (opening) { lists.push(tag); olNextIndex.push(Integer.valueOf(1)).toString();// TODO: add support for lists starting other index than 1 } else { lists.pop(); olNextIndex.pop().toString(); } } else if (tag.equalsIgnoreCase("li")) { if (opening) { if (output.length() > 0 && output.charAt(output.length() - 1) != '\n') { output.append("\n"); } String parentList = lists.peek(); if (parentList.equalsIgnoreCase("ol")) { start(output, new Ol()); output.append(olNextIndex.peek().toString() + ". "); olNextIndex.push(Integer.valueOf(olNextIndex.pop().intValue() + 1)); } else if (parentList.equalsIgnoreCase("ul")) { start(output, new Ul()); } } else { if (lists.peek().equalsIgnoreCase("ul")) { if (output.charAt(output.length() - 1) != '\n') { output.append("\n"); } // Nested BulletSpans increases distance between bullet and text, so we must prevent it. int bulletMargin = indent; if (lists.size() > 1) { bulletMargin = indent - bullet.getLeadingMargin(true); if (lists.size() > 2) { // This get's more complicated when we add a LeadingMarginSpan into the same line: // we have also counter it's effect to BulletSpan bulletMargin -= (lists.size() - 2) * listItemIndent; } } BulletSpan newBullet = new BulletSpan(bulletMargin); end(output, Ul.class, new LeadingMarginSpan.Standard(listItemIndent * (lists.size() - 1)), newBullet); } else if (lists.peek().equalsIgnoreCase("ol")) { if (output.charAt(output.length() - 1) != '\n') { output.append("\n"); } int numberMargin = listItemIndent * (lists.size() - 1); if (lists.size() > 2) { // Same as in ordered lists: counter the effect of nested Spans numberMargin -= (lists.size() - 2) * listItemIndent; } end(output, Ol.class, new LeadingMarginSpan.Standard(numberMargin)); } } } else if (tag.equalsIgnoreCase("code")) { if (opening) { output.setSpan(new TypefaceSpan("monospace"), output.length(), output.length(), Spannable.SPAN_MARK_MARK); } else { L.d("Code tag encountered"); Object obj = getLast(output, TypefaceSpan.class); int where = output.getSpanStart(obj); output.setSpan(new TypefaceSpan("monospace"), where, output.length(), 0); output.setSpan(new BackgroundColorSpan(Color.parseColor("#f1f1ff")), where, output.length(), 0); } } else { if (opening) L.d("Found an unsupported tag " + tag); } }