Example usage for android.text Selection getSelectionStart

List of usage examples for android.text Selection getSelectionStart

Introduction

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

Prototype

public static final int getSelectionStart(CharSequence text) 

Source Link

Document

Return the offset of the selection anchor 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, "");
    }//from w w w .  j ava2 s  .  com
    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);
    }//  w w w  . ja  v a 2  s.co 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:org.protocoder.fragments.EditorFragment.java

private int getCurrentCursorLine(Editable editable) {
    int selectionStartPos = Selection.getSelectionStart(editable);

    if (selectionStartPos < 0) {
        // There is no selection, so return -1 like getSelectionStart() does
        // when there is no seleciton.
        return -1;
    }/*w  w  w.  j  av  a 2 s . co m*/

    String preSelectionStartText = editable.toString().substring(0, selectionStartPos);
    return StringUtils.countMatches(preSelectionStartText, "\n");
}

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 ww . ja v  a2 s  .c om

    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./*from ww  w  .j av a 2s  .c  o  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;
}

From source file:nl.mpcjanssen.simpletask.AddTask.java

public int getCurrentCursorLine(@NotNull EditText editText) {
    int selectionStart = Selection.getSelectionStart(editText.getText());
    Layout layout = editText.getLayout();

    if (selectionStart != -1) {
        return layout.getLineForOffset(selectionStart);
    }/*from   ww w . j a v a2s  .  c  o m*/

    return -1;
}

From source file:com.sxt.superqq.activity.ChatActivity.java

/**
 * ?gridview?view/*from   w  w  w . j  a  v a 2  s  .co  m*/
 * 
 * @param i
 * @return
 */
private View getGridChildView(int i) {
    View view = View.inflate(this, R.layout.expression_gridview, null);
    ExpandGridView gv = (ExpandGridView) view.findViewById(R.id.gridview);
    List<String> list = new ArrayList<String>();
    if (i == 1) {
        List<String> list1 = reslist.subList(0, 20);
        list.addAll(list1);
    } else if (i == 2) {
        list.addAll(reslist.subList(20, reslist.size()));
    }
    list.add("delete_expression");
    final ExpressionAdapter expressionAdapter = new ExpressionAdapter(this, 1, list);
    gv.setAdapter(expressionAdapter);
    gv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String filename = expressionAdapter.getItem(position);
            try {
                // ????
                // ?????
                if (buttonSetModeKeyboard.getVisibility() != View.VISIBLE) {

                    if (filename != "delete_expression") { // ?
                        Editable text = mEditTextContent.getText();
                        int start = Selection.getSelectionStart(text);
                        text = text.insert(start, filename);
                        mEditTextContent.setText(EmotionUtils.replace(ChatActivity.this, text));
                        text = mEditTextContent.getText();
                        Selection.setSelection(text, start + filename.length());
                    } else { // 
                        if (!TextUtils.isEmpty(mEditTextContent.getText())) {

                            int selectionStart = mEditTextContent.getSelectionStart();// ??
                            if (selectionStart > 0) {
                                String body = mEditTextContent.getText().toString();
                                String tempStr = body.substring(0, selectionStart);
                                int i = tempStr.lastIndexOf("[");// ???
                                if (i != -1) {
                                    CharSequence cs = tempStr.substring(i, selectionStart);
                                    if (SmileUtils.containsKey(cs.toString()))
                                        mEditTextContent.getEditableText().delete(i, selectionStart);
                                    else
                                        mEditTextContent.getEditableText().delete(selectionStart - 1,
                                                selectionStart);
                                } else {
                                    mEditTextContent.getEditableText().delete(selectionStart - 1,
                                            selectionStart);
                                }
                            }
                        }

                    }
                }
            } catch (Exception e) {
            }
        }
    });
    return view;
}