Example usage for android.text Selection getSelectionEnd

List of usage examples for android.text Selection getSelectionEnd

Introduction

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

Prototype

public static final int getSelectionEnd(CharSequence text) 

Source Link

Document

Return the offset of the selection edge or cursor, or -1 if there is no selection or cursor.

Usage

From source file:Main.java

public static void insert(EditText input, CharSequence text) {
    int iCursorStart = Selection.getSelectionStart((input.getText()));
    int iCursorEnd = Selection.getSelectionEnd((input.getText()));
    if (iCursorStart != iCursorEnd) {
        ((Editable) input.getText()).replace(iCursorStart, iCursorEnd, "");
    }/*w  w  w .  j  a  va  2  s.c om*/
    int iCursor = Selection.getSelectionEnd((input.getText()));
    ((Editable) input.getText()).insert(iCursor, text);
}

From source file:nu.firetech.android.pactrack.frontend.ParcelIdDialog.java

@Override
public void onSaveInstanceState(Bundle outState) {
    if (mRowId != null) {
        outState.putLong(ParcelDbAdapter.KEY_ROWID, mRowId);
    }//from  w  w  w.j  a va  2 s  .  c  o m

    outState.putString(ParcelDbAdapter.KEY_PARCEL, mParcelText.getText().toString());
    outState.putString(ParcelDbAdapter.KEY_NAME, mNameText.getText().toString());

    View focusedView = getDialog().getCurrentFocus();
    if (focusedView != null && focusedView.getId() != View.NO_ID) {
        outState.putInt(KEY_FOCUSED_FIELD, focusedView.getId());
        if (focusedView instanceof EditText) {
            outState.putInt(KEY_SELECTION_START,
                    Selection.getSelectionStart(((EditText) focusedView).getText()));
            outState.putInt(KEY_SELECTION_END, Selection.getSelectionEnd(((EditText) focusedView).getText()));
        }
    }
}

From source file:android.support.text.emoji.EmojiProcessor.java

private static boolean delete(final Editable content, final KeyEvent event, final boolean forwardDelete) {
    if (hasModifiers(event)) {
        return false;
    }/*from   w  w  w. j av  a2 s  .c  o m*/

    final int start = Selection.getSelectionStart(content);
    final int end = Selection.getSelectionEnd(content);
    if (hasInvalidSelection(start, end)) {
        return false;
    }

    final EmojiSpan[] spans = content.getSpans(start, end, EmojiSpan.class);
    if (spans != null && spans.length > 0) {
        final int length = spans.length;
        for (int index = 0; index < length; index++) {
            final EmojiSpan span = spans[index];
            final int spanStart = content.getSpanStart(span);
            final int spanEnd = content.getSpanEnd(span);
            if ((forwardDelete && spanStart == start) || (!forwardDelete && spanEnd == start)
                    || (start > spanStart && start < spanEnd)) {
                content.delete(spanStart, spanEnd);
                return true;
            }
        }
    }

    return false;
}

From source file:android.support.text.emoji.EmojiProcessor.java

/**
 * Handles deleteSurroundingText commands from {@link InputConnection} and tries to delete an
 * {@link EmojiSpan} from an {@link Editable}. Returns {@code true} if an {@link EmojiSpan} is
 * deleted.//ww  w.  j  a  v  a  2 s . co  m
 * <p/>
 * If there is a selection where selection start is not equal to selection end, does not
 * delete.
 *
 * @param inputConnection InputConnection instance
 * @param editable TextView.Editable instance
 * @param beforeLength the number of characters before the cursor to be deleted
 * @param afterLength the number of characters after the cursor to be deleted
 * @param inCodePoints {@code true} if length parameters are in codepoints
 *
 * @return {@code true} if an {@link EmojiSpan} is deleted
 */
static boolean handleDeleteSurroundingText(@NonNull final InputConnection inputConnection,
        @NonNull final Editable editable, @IntRange(from = 0) final int beforeLength,
        @IntRange(from = 0) final int afterLength, final boolean inCodePoints) {
    //noinspection ConstantConditions
    if (editable == null || inputConnection == null) {
        return false;
    }

    if (beforeLength < 0 || afterLength < 0) {
        return false;
    }

    final int selectionStart = Selection.getSelectionStart(editable);
    final int selectionEnd = Selection.getSelectionEnd(editable);

    if (hasInvalidSelection(selectionStart, selectionEnd)) {
        return false;
    }

    int start;
    int end;
    if (inCodePoints) {
        // go backwards in terms of codepoints
        start = CodepointIndexFinder.findIndexBackward(editable, selectionStart, Math.max(beforeLength, 0));
        end = CodepointIndexFinder.findIndexForward(editable, selectionEnd, Math.max(afterLength, 0));

        if (start == CodepointIndexFinder.INVALID_INDEX || end == CodepointIndexFinder.INVALID_INDEX) {
            return false;
        }
    } else {
        start = Math.max(selectionStart - beforeLength, 0);
        end = Math.min(selectionEnd + afterLength, editable.length());
    }

    final EmojiSpan[] spans = editable.getSpans(start, end, EmojiSpan.class);
    if (spans != null && spans.length > 0) {
        final int length = spans.length;
        for (int index = 0; index < length; index++) {
            final EmojiSpan span = spans[index];
            int spanStart = editable.getSpanStart(span);
            int spanEnd = editable.getSpanEnd(span);
            start = Math.min(spanStart, start);
            end = Math.max(spanEnd, end);
        }

        start = Math.max(start, 0);
        end = Math.min(end, editable.length());

        inputConnection.beginBatchEdit();
        editable.delete(start, end);
        inputConnection.endBatchEdit();
        return true;
    }

    return false;
}