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:com.fenlisproject.elf.core.widget.ExtendedEditText.java

/**
 * Convenience for {@link Selection#setSelection(Spannable, int, int)}.
 *//*w  w w .ja  v a  2 s. c om*/
public void setSelection(int start, int stop) {
    Selection.setSelection(getText(), start, stop);
}

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

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);

    dialog.setContentView(R.layout.parcel_id_dialog);
    dialog.setTitle(R.string.menu_add_parcel);

    mErrorDialog = new AlertDialog.Builder(getActivity()).setTitle(R.string.id_error_title)
            .setIconAttribute(android.R.attr.alertDialogIcon).setMessage(R.string.id_error_message)
            .setNeutralButton(R.string.ok, new OnClickListener() {
                @Override/*from w  w  w .  j a  va 2 s .  c o m*/
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).create();

    mDbAdapter = new ParcelDbAdapter(getActivity()).open();

    mParcelText = (EditText) dialog.findViewById(R.id.parcelid);
    mParcelText.setKeyListener(new NumberKeyListener() {
        private char[] acceptedChars = null;

        @Override
        protected char[] getAcceptedChars() {
            if (acceptedChars == null) {
                acceptedChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
                        'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                        'U', 'V', 'W', 'X', 'Y', 'Z' };
            }

            return acceptedChars;
        }

        @Override
        public int getInputType() {
            return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS;
        }
    });
    mNameText = (EditText) dialog.findViewById(R.id.parcelname);

    ImageButton scanButton = (ImageButton) dialog.findViewById(R.id.barcode);
    scanButton.setOnClickListener(new ScanButtonListener());
    Button cancelButton = (Button) dialog.findViewById(R.id.cancel);
    cancelButton.setOnClickListener(new ClosingButtonListener());
    Button okButton = (Button) dialog.findViewById(R.id.ok);
    okButton.setOnClickListener(new OkListener());

    if (savedInstanceState != null && savedInstanceState.containsKey(ParcelDbAdapter.KEY_ROWID)) {
        mRowId = savedInstanceState.getLong(ParcelDbAdapter.KEY_ROWID);
    }

    boolean loadParcel = false;

    mParcelInitialText = "";
    if (savedInstanceState != null && savedInstanceState.containsKey(ParcelDbAdapter.KEY_PARCEL)) {
        mParcelInitialText = savedInstanceState.getString(ParcelDbAdapter.KEY_PARCEL);
    } else if (mRowId != null) {
        loadParcel = true;
        mParcelInitialText = getString(R.string.loading);
        mParcelText.setEnabled(false);
    }
    mParcelText.setText(mParcelInitialText);

    mNameInitialText = "";
    if (savedInstanceState != null && savedInstanceState.containsKey(ParcelDbAdapter.KEY_NAME)) {
        mNameInitialText = savedInstanceState.getString(ParcelDbAdapter.KEY_NAME);
    } else if (mRowId != null) {
        loadParcel = true;
        mNameInitialText = getString(R.string.loading);
        mNameText.setEnabled(false);
    }
    mNameText.setText(mNameInitialText);

    mFocusedView = null;
    if (savedInstanceState != null && savedInstanceState.containsKey(KEY_FOCUSED_FIELD)) {
        mFocusedView = dialog.findViewById(savedInstanceState.getInt(KEY_FOCUSED_FIELD));

        mInitialSelectionStart = mInitialSelectionEnd = 0;
        if (mFocusedView instanceof EditText && savedInstanceState.containsKey(KEY_SELECTION_START)
                && savedInstanceState.containsKey(KEY_SELECTION_END)) {
            mInitialSelectionStart = savedInstanceState.getInt(KEY_SELECTION_START);
            mInitialSelectionEnd = savedInstanceState.getInt(KEY_SELECTION_END);

            Selection.setSelection(((EditText) mFocusedView).getText(), mInitialSelectionStart,
                    mInitialSelectionEnd);
        }
        mFocusedView.requestFocus();
    }

    if (loadParcel) {
        getLoaderManager().initLoader(INITIAL_LOADER_ID, null, this);
    }

    return dialog;
}

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

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    switch (loader.getId()) {
    case INITIAL_LOADER_ID:
        if (!mParcelText.isEnabled()) {
            mParcelText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ParcelDbAdapter.KEY_PARCEL)));
            mParcelText.setEnabled(true);
        }//  w  w  w . jav a  2  s.c  o  m
        if (!mNameText.isEnabled()) {
            mNameText.setText(cursor.getString(cursor.getColumnIndexOrThrow(ParcelDbAdapter.KEY_NAME)));
            mNameText.setEnabled(true);
        }
        if (mFocusedView != null) {
            if (mFocusedView instanceof EditText) {
                Selection.setSelection(((EditText) mFocusedView).getText(), mInitialSelectionStart,
                        mInitialSelectionEnd);
            }
            mFocusedView.requestFocus();
        }
        break;
    }
}

From source file:cc.flydev.launcher.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);/*w w w .j  a  v  a  2s  .  c om*/
    // Convert to a string here to ensure that no other state associated with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    mInfo.setTitle(newTitle);
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);

    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on ourself. This
    // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
    requestFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}

From source file:com.llf.android.launcher3.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);//from  w  ww.ja  v  a2  s.c  o  m
    // Convert to a string here to ensure that no other state associated
    // with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    mInfo.setTitle(newTitle);
    LauncherModel.updateItemInDatabase(getContext(), mInfo);

    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on
    // ourself. This
    // ensures that every time the field is clicked, focus is gained, giving
    // reliable behavior.
    requestFocus();

    Selection.setSelection(mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}

From source file:com.zyk.launcher.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);/*from  w  w  w  .ja va 2s  .  co m*/
    // Convert to a string here to ensure that no other state associated with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    mInfo.setTitle(newTitle);
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);
    System.out.println("doneEditingFolderName mInfo:" + mInfo);
    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on ourself. This
    // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
    requestFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}

From source file:com.aidy.launcher3.ui.folder.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);/*from   www  .  j  ava  2 s.  com*/
    // Convert to a string here to ensure that no other state associated
    // with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    mInfo.setTitle(newTitle);
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);

    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on
    // ourself. This
    // ensures that every time the field is clicked, focus is gained, giving
    // reliable behavior.
    requestFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}

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 ww . j ava 2  s .  c om
@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:com.phonemetra.turbo.launcher.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);//from   w w  w .  j av  a 2  s  .  c o m
    // Convert to a string here to ensure that no other state associated with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    if (!SettingsProvider.getBoolean(mLauncher, SettingsProvider.SETTINGS_UI_HOMESCREEN_HIDE_ICON_LABELS,
            R.bool.preferences_interface_homescreen_hide_icon_labels_default)) {
        mInfo.setTitle(newTitle);
    }
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);

    if (commit) {
        sendCustomAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                String.format(getContext().getString(R.string.folder_renamed), newTitle));
    }
    // In order to clear the focus from the text field, we set the focus on ourself. This
    // ensures that every time the field is clicked, focus is gained, giving reliable behavior.
    requestFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}

From source file:com.android.launcher3.folder.Folder.java

public void doneEditingFolderName(boolean commit) {
    mFolderName.setHint(sHintText);// w  ww .j  a va 2  s.  c o  m
    // Convert to a string here to ensure that no other state associated with the text field
    // gets saved.
    String newTitle = mFolderName.getText().toString();
    mInfo.setTitle(newTitle);
    LauncherModel.updateItemInDatabase(mLauncher, mInfo);

    if (commit) {
        Utilities.sendCustomAccessibilityEvent(this, AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED,
                getContext().getString(R.string.folder_renamed, newTitle));
    }

    // This ensures that focus is gained every time the field is clicked, which selects all
    // the text and brings up the soft keyboard if necessary.
    mFolderName.clearFocus();

    Selection.setSelection((Spannable) mFolderName.getText(), 0, 0);
    mIsEditingName = false;
}