List of usage examples for android.text Selection removeSelection
public static final void removeSelection(Spannable text)
From source file:com.vuze.android.remote.spanbubbles.SpanTags.java
public void setTagBubbles(final SpannableStringBuilder ss, String text, String token) { if (ss.length() > 0) { // hack to ensure descent is always added by TextView ss.append("\u200B"); }/* w ww. j a v a 2s. com*/ if (tvTags == null) { Log.e(TAG, "no tvTags"); return; } TextPaint p = tvTags.getPaint(); // Start and end refer to the points where the span will apply int tokenLen = token.length(); int base = 0; if (showIcon && tagDrawables == null) { createDrawTagables(); } while (true) { int start = text.indexOf(token, base); int end = text.indexOf(token, start + tokenLen); if (start < 0 || end < 0) { break; } base = end + tokenLen; final int fSpanStart = start; final int fSpanEnd = end + tokenLen; String id = text.substring(start + tokenLen, end); Map mapTag = null; try { long tagUID = Long.parseLong(id); mapTag = sessionInfo.getTag(tagUID); } catch (Throwable ignore) { } final String word = MapUtils.getMapString(mapTag, "name", "" + id); final Map fMapTag = mapTag; final DrawableTag imgDrawable = new DrawableTag(context, p, word, showIcon ? tagDrawables : null, mapTag, drawCount) { @Override public boolean isTagPressed() { if (!AndroidUtils.usesNavigationControl()) { return false; } int selectionEnd = tvTags.getSelectionEnd(); if (selectionEnd < 0) { return false; } int selectionStart = tvTags.getSelectionStart(); return selectionStart == fSpanStart && selectionEnd == fSpanEnd; } @Override public int getTagState() { if (listener == null) { return TAG_STATE_SELECTED; } return listener.getTagState(fMapTag, word); } }; if (countFontRatio > 0) { imgDrawable.setCountFontRatio(countFontRatio); } imgDrawable.setBounds(0, 0, imgDrawable.getIntrinsicWidth(), imgDrawable.getIntrinsicHeight()); // Log.d(TAG, "State=" + Arrays.toString(imgDrawable.getState())); if (listener != null && showIcon) { int tagState = listener.getTagState(mapTag, word); int[] state = makeState(tagState, mapTag == null, false); imgDrawable.setState(state); } ImageSpan imageSpan = new ImageSpan(imgDrawable, DynamicDrawableSpan.ALIGN_BASELINE) { @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { int size = super.getSize(paint, text, start, end, fm); int width = -1; if (tvTags.getLayoutParams().width == ViewGroup.LayoutParams.WRAP_CONTENT) { if (tvTags.getParent() instanceof ViewGroup) { width = ((ViewGroup) tvTags.getParent()).getWidth(); } } else { width = tvTags.getWidth(); } if (width <= 0) { return size; } return Math.min(size, width); } }; ss.setSpan(imageSpan, start, end + tokenLen, 0); if (listener != null) { ClickableSpan clickSpan = new ClickableSpan() { @Override public void onClick(View widget) { listener.tagClicked(fMapTag, word); if (AndroidUtils.hasTouchScreen()) { Selection.removeSelection((Spannable) tvTags.getText()); } widget.invalidate(); } }; ss.setSpan(clickSpan, start, end + tokenLen, 0); } } }
From source file:com.tct.mail.browse.SubjectAndFolderView.java
@Override public Parcelable onSaveInstanceState() { if (getText() instanceof Spannable) { Selection.removeSelection((Spannable) getText()); }//ww w .ja v a2 s.c o m return super.onSaveInstanceState(); }
From source file:com.amitupadhyay.aboutexample.ui.widget.FabOverlapTextView.java
/** * This is why you don't implement your own TextView kids; you have to handle everything! *//*from w w w .ja v a 2 s . c o m*/ @Override public boolean onTouchEvent(MotionEvent event) { if (!(text instanceof Spanned)) return super.onTouchEvent(event); Spannable spannedText = (Spannable) text; boolean handled = false; if (event.getAction() == MotionEvent.ACTION_DOWN) { pressedSpan = getPressedSpan(spannedText, event); if (pressedSpan != null) { pressedSpan.setPressed(true); Selection.setSelection(spannedText, spannedText.getSpanStart(pressedSpan), spannedText.getSpanEnd(pressedSpan)); handled = true; postInvalidateOnAnimation(); } } else if (event.getAction() == MotionEvent.ACTION_MOVE) { TouchableUrlSpan touchedSpan = getPressedSpan(spannedText, event); if (pressedSpan != null && touchedSpan != pressedSpan) { pressedSpan.setPressed(false); pressedSpan = null; Selection.removeSelection(spannedText); postInvalidateOnAnimation(); } } else if (event.getAction() == MotionEvent.ACTION_UP) { if (pressedSpan != null) { pressedSpan.setPressed(false); pressedSpan.onClick(this); handled = true; postInvalidateOnAnimation(); } pressedSpan = null; Selection.removeSelection(spannedText); } else { if (pressedSpan != null) { pressedSpan.setPressed(false); handled = true; postInvalidateOnAnimation(); } pressedSpan = null; Selection.removeSelection(spannedText); } return handled; }
From source file:android.support.v17.leanback.widget.GridWidgetTest.java
public void testNoDispatchSaveChildState() throws Throwable { mInstrumentation = getInstrumentation(); Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class); intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear); int[] items = new int[100]; for (int i = 0; i < items.length; i++) { items[i] = 200;/*from w ww . j ava2 s. com*/ } intent.putExtra(GridActivity.EXTRA_ITEMS, items); intent.putExtra(GridActivity.EXTRA_STAGGERED, false); intent.putExtra(GridActivity.EXTRA_CHILD_LAYOUT_ID, R.layout.selectable_text_view); mOrientation = BaseGridView.VERTICAL; mNumRows = 1; initActivity(intent); mGridView.setSaveChildrenPolicy(VerticalGridView.SAVE_NO_CHILD); final SparseArray<Parcelable> container = new SparseArray<Parcelable>(); // 1. Set text selection, save view states should do nothing on child runTestOnUiThread(new Runnable() { public void run() { for (int i = 0; i < mGridView.getChildCount(); i++) { Selection.setSelection((Spannable) (((TextView) mGridView.getChildAt(i)).getText()), 0, 1); } mGridView.saveHierarchyState(container); } }); // 2. clear the text selection runTestOnUiThread(new Runnable() { public void run() { for (int i = 0; i < mGridView.getChildCount(); i++) { Selection.removeSelection((Spannable) (((TextView) mGridView.getChildAt(i)).getText())); } } }); // 3. Restore view states should be a no-op for child runTestOnUiThread(new Runnable() { public void run() { mGridView.restoreHierarchyState(container); for (int i = 0; i < mGridView.getChildCount(); i++) { assertEquals(-1, ((TextView) mGridView.getChildAt(i)).getSelectionStart()); assertEquals(-1, ((TextView) mGridView.getChildAt(i)).getSelectionEnd()); } } }); }