List of usage examples for android.text Spannable getSpanStart
public int getSpanStart(Object tag);
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Remove the chip and any text associated with it from the RecipientEditTextView. * * @param alsoNotifyAboutDataChanges/*from w ww. j a v a 2s . c om*/ */ // Visible for testing. /* package */void removeChip(final DrawableRecipientChip chip, final boolean alsoNotifyAboutDataChanges) { if (!alsoNotifyAboutDataChanges) --mPreviousChipsCount; final Spannable spannable = getSpannable(); final int spanStart = spannable.getSpanStart(chip); final int spanEnd = spannable.getSpanEnd(chip); final Editable text = getText(); int toDelete = spanEnd; final boolean wasSelected = chip == mSelectedChip; // Clear that there is a selected chip before updating any text. if (wasSelected) mSelectedChip = null; // Always remove trailing spaces when removing a chip. while (toDelete >= 0 && toDelete < text.length() && text.charAt(toDelete) == ' ') toDelete++; spannable.removeSpan(chip); if (spanStart >= 0 && toDelete > 0) text.delete(spanStart, toDelete); if (wasSelected) clearSelectedChip(); }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Show specified chip as selected. If the RecipientChip is just an email address, selecting the chip will take the * contents of the chip and place it at the end of the RecipientEditTextView for inline editing. If the * RecipientChip is a complete contact, then selecting the chip will change the background color of the chip, show * the delete icon, and a popup window with the address in use highlighted and any other alternate addresses for the * contact.//from w w w . j a v a 2 s. com * * @param currentChip * Chip to select. * @return A RecipientChip in the selected state or null if the chip just contained an email address. */ private DrawableRecipientChip selectChip(final DrawableRecipientChip currentChip) { if (shouldShowEditableText(currentChip)) { final CharSequence text = currentChip.getValue(); final Editable editable = getText(); final Spannable spannable = getSpannable(); final int spanStart = spannable.getSpanStart(currentChip); final int spanEnd = spannable.getSpanEnd(currentChip); spannable.removeSpan(currentChip); editable.delete(spanStart, spanEnd); setCursorVisible(true); setSelection(editable.length()); editable.append(text); return constructChipSpan(RecipientEntry.constructFakeEntry((String) text, isValid(text.toString())), true, false); } else if (currentChip.getContactId() == RecipientEntry.GENERATED_CONTACT || currentChip.isGalContact()) { final int start = getChipStart(currentChip); final int end = getChipEnd(currentChip); getSpannable().removeSpan(currentChip); DrawableRecipientChip newChip; try { if (mNoChips) return null; newChip = constructChipSpan(currentChip.getEntry(), true, false); } catch (final NullPointerException e) { Log.e(TAG, e.getMessage(), e); return null; } final Editable editable = getText(); QwertyKeyListener.markAsReplaced(editable, start, end, ""); if (start == -1 || end == -1) Log.d(TAG, "The chip being selected no longer exists but should."); else editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); newChip.setSelected(true); if (shouldShowEditableText(newChip)) scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip))); //showAddress(newChip,mAddressPopup,getWidth()); setCursorVisible(false); return newChip; } else { final int start = getChipStart(currentChip); final int end = getChipEnd(currentChip); getSpannable().removeSpan(currentChip); DrawableRecipientChip newChip; try { newChip = constructChipSpan(currentChip.getEntry(), true, false); } catch (final NullPointerException e) { Log.e(TAG, e.getMessage(), e); return null; } final Editable editable = getText(); QwertyKeyListener.markAsReplaced(editable, start, end, ""); if (start == -1 || end == -1) Log.d(TAG, "The chip being selected no longer exists but should."); else editable.setSpan(newChip, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); newChip.setSelected(true); if (shouldShowEditableText(newChip)) scrollLineIntoView(getLayout().getLineForOffset(getChipStart(newChip))); //showAlternates(newChip,mAlternatesPopup,getWidth()); setCursorVisible(false); return newChip; } }
From source file:com.android.ex.chips.RecipientEditTextView.java
DrawableRecipientChip[] getSortedRecipients() { final DrawableRecipientChip[] recips = getSpannable().getSpans(0, getText().length(), DrawableRecipientChip.class); final ArrayList<DrawableRecipientChip> recipientsList = new ArrayList<DrawableRecipientChip>( Arrays.asList(recips)); final Spannable spannable = getSpannable(); Collections.sort(recipientsList, new Comparator<DrawableRecipientChip>() { @Override/* w w w . j av a2 s .c o m*/ public int compare(final DrawableRecipientChip first, final DrawableRecipientChip second) { final int firstStart = spannable.getSpanStart(first); final int secondStart = spannable.getSpanStart(second); if (firstStart < secondStart) return -1; else if (firstStart > secondStart) return 1; else return 0; } }); return recipientsList.toArray(new DrawableRecipientChip[recipientsList.size()]); }
From source file:com.android.ex.chips.RecipientEditTextView.java
/** * Create the more chip. The more chip is text that replaces any chips that do not fit in the pre-defined available * space when the RecipientEditTextView loses focus. *///from ww w .jav a 2 s . c o m // Visible for testing. /* package */void createMoreChip() { if (mNoChips) { createMoreChipPlainText(); return; } if (!mShouldShrink) return; final ImageSpan[] tempMore = getSpannable().getSpans(0, getText().length(), MoreImageSpan.class); if (tempMore.length > 0) getSpannable().removeSpan(tempMore[0]); final DrawableRecipientChip[] recipients = getSortedRecipients(); if (recipients == null || recipients.length <= CHIP_LIMIT) { mMoreChip = null; return; } final Spannable spannable = getSpannable(); final int numRecipients = recipients.length; final int overage = numRecipients - CHIP_LIMIT; final MoreImageSpan moreSpan = createMoreSpan(overage); mRemovedSpans = new ArrayList<DrawableRecipientChip>(); int totalReplaceStart = 0; int totalReplaceEnd = 0; final Editable text = getText(); for (int i = numRecipients - overage; i < recipients.length; i++) { mRemovedSpans.add(recipients[i]); if (i == numRecipients - overage) totalReplaceStart = spannable.getSpanStart(recipients[i]); if (i == recipients.length - 1) totalReplaceEnd = spannable.getSpanEnd(recipients[i]); if (mTemporaryRecipients == null || !mTemporaryRecipients.contains(recipients[i])) { final int spanStart = spannable.getSpanStart(recipients[i]); final int spanEnd = spannable.getSpanEnd(recipients[i]); recipients[i].setOriginalText(text.toString().substring(spanStart, spanEnd)); } spannable.removeSpan(recipients[i]); } if (totalReplaceEnd < text.length()) totalReplaceEnd = text.length(); final int end = Math.max(totalReplaceStart, totalReplaceEnd); final int start = Math.min(totalReplaceStart, totalReplaceEnd); final SpannableString chipText = new SpannableString(text.subSequence(start, end)); chipText.setSpan(moreSpan, 0, chipText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); text.replace(start, end, chipText); mMoreChip = moreSpan; // If adding the +more chip goes over the limit, resize accordingly. if (!isPhoneQuery() && getLineCount() > mMaxLines) setMaxLines(getLineCount()); }
From source file:org.telegram.ui.ArticleViewer.java
private boolean checkLayoutForLinks(MotionEvent event, View parentView, StaticLayout layout, int layoutX, int layoutY) { if (parentView == null || layout == null) { return false; }/*from ww w . j a v a2 s . c o m*/ CharSequence text = layout.getText(); if (!(text instanceof Spannable)) { return false; } int x = (int) event.getX(); int y = (int) event.getY(); boolean removeLink = false; if (event.getAction() == MotionEvent.ACTION_DOWN) { if (x >= layoutX && x <= layoutX + layout.getWidth() && y >= layoutY && y <= layoutY + layout.getHeight()) { try { int checkX = x - layoutX; int checkY = y - layoutY; final int line = layout.getLineForVertical(checkY); final int off = layout.getOffsetForHorizontal(line, checkX); final float left = layout.getLineLeft(line); if (left <= checkX && left + layout.getLineWidth(line) >= checkX) { Spannable buffer = (Spannable) layout.getText(); TextPaintUrlSpan[] link = buffer.getSpans(off, off, TextPaintUrlSpan.class); if (link != null && link.length > 0) { pressedLink = link[0]; int pressedStart = buffer.getSpanStart(pressedLink); int pressedEnd = buffer.getSpanEnd(pressedLink); for (int a = 1; a < link.length; a++) { TextPaintUrlSpan span = link[a]; int start = buffer.getSpanStart(span); int end = buffer.getSpanEnd(span); if (pressedStart > start || end > pressedEnd) { pressedLink = span; pressedStart = start; pressedEnd = end; } } pressedLinkOwnerLayout = layout; pressedLinkOwnerView = parentView; try { urlPath.setCurrentLayout(layout, pressedStart, 0); layout.getSelectionPath(pressedStart, pressedEnd, urlPath); parentView.invalidate(); } catch (Exception e) { FileLog.e(e); } } } } catch (Exception e) { FileLog.e(e); } } } else if (event.getAction() == MotionEvent.ACTION_UP) { if (pressedLink != null) { removeLink = true; String url = pressedLink.getUrl(); if (url != null) { int index; boolean isAnchor = false; final String anchor; if ((index = url.lastIndexOf('#')) != -1) { anchor = url.substring(index + 1); if (url.toLowerCase().contains(currentPage.url.toLowerCase())) { Integer row = anchors.get(anchor); if (row != null) { layoutManager.scrollToPositionWithOffset(row, 0); isAnchor = true; } } } else { anchor = null; } if (!isAnchor) { if (openUrlReqId == 0) { showProgressView(true); final TLRPC.TL_messages_getWebPage req = new TLRPC.TL_messages_getWebPage(); req.url = pressedLink.getUrl(); req.hash = 0; openUrlReqId = ConnectionsManager.getInstance().sendRequest(req, new RequestDelegate() { @Override public void run(final TLObject response, TLRPC.TL_error error) { AndroidUtilities.runOnUIThread(new Runnable() { @Override public void run() { if (openUrlReqId == 0) { return; } openUrlReqId = 0; showProgressView(false); if (isVisible) { if (response instanceof TLRPC.TL_webPage && ((TLRPC.TL_webPage) response).cached_page instanceof TLRPC.TL_pageFull) { addPageToStack((TLRPC.TL_webPage) response, anchor); } else { Browser.openUrl(parentActivity, req.url); } } } }); } }); } } } } } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { removeLink = true; } if (removeLink && pressedLink != null) { pressedLink = null; pressedLinkOwnerLayout = null; pressedLinkOwnerView = null; parentView.invalidate(); } if (pressedLink != null && event.getAction() == MotionEvent.ACTION_DOWN) { startCheckLongPress(); } if (event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE) { cancelCheckLongPress(); } return pressedLink != null; }