Example usage for android.text Selection setSelection

List of usage examples for android.text Selection setSelection

Introduction

In this page you can find the example usage for android.text Selection setSelection.

Prototype

public static void setSelection(Spannable text, int start, int stop) 

Source Link

Document

Set the selection anchor to start and the selection edge to stop.

Usage

From source file:android.support.v17.leanback.widget.GridWidgetTest.java

public void testChildStates() 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 ww w .ja v a2 s  .c  om*/
    }
    intent.putExtra(GridActivity.EXTRA_ITEMS, items);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    intent.putExtra(GridActivity.EXTRA_REQUEST_LAYOUT_ONFOCUS, true);
    intent.putExtra(GridActivity.EXTRA_CHILD_LAYOUT_ID, R.layout.selectable_text_view);
    mOrientation = BaseGridView.VERTICAL;
    mNumRows = 1;

    initActivity(intent);
    mGridView.setSaveChildrenPolicy(VerticalGridView.SAVE_ALL_CHILD);

    final SparseArray<Parcelable> container = new SparseArray<Parcelable>();

    // 1 Save view states
    runTestOnUiThread(new Runnable() {
        public void run() {
            Selection.setSelection((Spannable) (((TextView) mGridView.getChildAt(0)).getText()), 0, 1);
            Selection.setSelection((Spannable) (((TextView) mGridView.getChildAt(1)).getText()), 0, 1);
            mGridView.saveHierarchyState(container);
        }
    });

    // 2 Change view states
    runTestOnUiThread(new Runnable() {
        public void run() {
            Selection.setSelection((Spannable) (((TextView) mGridView.getChildAt(0)).getText()), 1, 2);
            Selection.setSelection((Spannable) (((TextView) mGridView.getChildAt(1)).getText()), 1, 2);
        }
    });

    // 3 Detached and re-attached,  should still maintain state of (2)
    runTestOnUiThread(new Runnable() {
        public void run() {
            mGridView.setSelectedPositionSmooth(1);
        }
    });
    waitForScrollIdleAndItemAnimation(mVerifyLayout);
    assertEquals(((TextView) mGridView.getChildAt(0)).getSelectionStart(), 1);
    assertEquals(((TextView) mGridView.getChildAt(0)).getSelectionEnd(), 2);
    assertEquals(((TextView) mGridView.getChildAt(1)).getSelectionStart(), 1);
    assertEquals(((TextView) mGridView.getChildAt(1)).getSelectionEnd(), 2);

    // 4 Recycled and rebound, should load state from (2)
    runTestOnUiThread(new Runnable() {
        public void run() {
            mGridView.setSelectedPositionSmooth(20);
        }
    });
    waitForScrollIdle(mVerifyLayout);
    runTestOnUiThread(new Runnable() {
        public void run() {
            mGridView.setSelectedPositionSmooth(0);
        }
    });
    waitForScrollIdleAndItemAnimation(mVerifyLayout);
    assertEquals(((TextView) mGridView.getChildAt(0)).getSelectionStart(), 1);
    assertEquals(((TextView) mGridView.getChildAt(0)).getSelectionEnd(), 2);
    assertEquals(((TextView) mGridView.getChildAt(1)).getSelectionStart(), 1);
    assertEquals(((TextView) mGridView.getChildAt(1)).getSelectionEnd(), 2);
}

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  www  .j av  a 2  s .co m
    }
    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());
            }
        }
    });
}