Example usage for android.widget TextView getSelectionEnd

List of usage examples for android.widget TextView getSelectionEnd

Introduction

In this page you can find the example usage for android.widget TextView getSelectionEnd.

Prototype

@ViewDebug.ExportedProperty(category = "text")
public int getSelectionEnd() 

Source Link

Document

Convenience for Selection#getSelectionEnd .

Usage

From source file:com.android.talkback.controller.CursorControllerAppTest.java

@MediumTest
public void testSetSelectionModeActive() {
    setContentView(R.layout.text_activity);

    final TextView usernameView = (TextView) getViewForId(R.id.username);

    getInstrumentation().runOnMainSync(new Runnable() {
        @Override// w ww  .  j av  a 2s.co  m
        public void run() {
            usernameView.setText("abcdefghijklmnop");
        }
    });
    getInstrumentation().waitForIdleSync();
    waitForAccessibilityIdleSync();

    AccessibilityNodeInfoCompat username = getNodeForId(R.id.username);
    mCursorController.setCursor(username);
    mCursorController.setGranularity(CursorGranularity.CHARACTER, false);
    waitForAccessibilityIdleSync();

    // Move cursor between "c" and "d".
    for (int i = 0; i < 3; ++i) {
        mCursorController.next(false, false, false, InputModeManager.INPUT_MODE_TOUCH);
        waitForAccessibilityIdleSync();
    }

    mCursorController.setSelectionModeActive(username, true);
    waitForAccessibilityIdleSync();

    // Select five characters "defgh".
    for (int i = 0; i < 5; ++i) {
        mCursorController.next(false, false, false, InputModeManager.INPUT_MODE_TOUCH);
        waitForAccessibilityIdleSync();
    }

    assertEquals(3, usernameView.getSelectionStart());
    assertEquals(8, usernameView.getSelectionEnd()); // 8-3 = 5 char selection.
}

From source file:com.android.mms.ui.ComposeMessageActivity.java

private Uri getSelectedUriFromMessageList(ListView listView, int position) {
    // If the context menu was opened over a uri, get that uri.
    MessageListItem msglistItem = (MessageListItem) listView.getChildAt(position);
    if (msglistItem == null) {
        // FIXME: Should get the correct view. No such interface in ListView currently
        // to get the view by position. The ListView.getChildAt(position) cannot
        // get correct view since the list doesn't create one child for each item.
        // And if setSelection(position) then getSelectedView(),
        // cannot get corrent view when in touch mode.
        return null;
    }/*from  www . j a  va 2s .  c om*/

    TextView textView;
    CharSequence text = null;
    int selStart = -1;
    int selEnd = -1;

    //check if message sender is selected
    textView = (TextView) msglistItem.findViewById(R.id.text_view);
    if (textView != null) {
        text = textView.getText();
        selStart = textView.getSelectionStart();
        selEnd = textView.getSelectionEnd();
    }

    // Check that some text is actually selected, rather than the cursor
    // just being placed within the TextView.
    if (selStart != selEnd) {
        int min = Math.min(selStart, selEnd);
        int max = Math.max(selStart, selEnd);

        URLSpan[] urls = ((Spanned) text).getSpans(min, max, URLSpan.class);

        if (urls.length == 1) {
            return Uri.parse(urls[0].getURL());
        }
    }

    //no uri was selected
    return null;
}