Example usage for android.widget EditText setSelection

List of usage examples for android.widget EditText setSelection

Introduction

In this page you can find the example usage for android.widget EditText setSelection.

Prototype

public void setSelection(int start, int stop) 

Source Link

Document

Convenience for Selection#setSelection(Spannable,int,int) .

Usage

From source file:Main.java

/**
 * Substitute the selection of given {@link EditText} with specified text.<br />
 * If no text is selected then specified text will be inserted at current cursor position.<br />
 * Newly inserted text will be selected.
 * @param editText Target {@link EditText}.
 * @param txt Text to insert.//from  www .  ja  va2 s  .com
 * @param select {@code true} to select newly added text.
 */
public static void setTextInsideSelection(EditText editText, String txt, boolean select) {
    Editable editable;
    int selStart;
    int selEnd;

    editable = editText.getText();

    selStart = editText.getSelectionStart();
    selEnd = editText.getSelectionEnd();
    if (selStart > selEnd) {
        int tmp = selStart;
        selStart = selEnd;
        selEnd = tmp;
    }

    editable.replace(selStart, selEnd, txt);

    if (select) {
        editText.setSelection(selStart, selStart + txt.length());
    } else {
        editText.setSelection(selStart + txt.length());
    }
}

From source file:com.anjalimacwan.activity.NoteEditActivity.java

@Override
protected void onStart() {
    super.onStart();

    // Set text in EditView
    if (external != null) {
        EditText noteContents = (EditText) findViewById(R.id.editText1);
        noteContents.setText(external);//from w ww.j a  va2 s .  c o m
        noteContents.setSelection(external.length(), external.length());
    }
}

From source file:com.farmerbb.notepad.activity.NoteEditActivity.java

@Override
protected void onStart() {
    super.onStart();

    // Set text in EditView
    if (external != null) {
        EditText noteContents = findViewById(R.id.editText1);
        noteContents.setText(external);/*from ww w . j  av  a 2  s  .  c  o m*/
        noteContents.setSelection(external.length(), external.length());
    }
}

From source file:org.geometerplus.android.fbreader.bookmark.EditBookmarkActivity.java

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.edit_bookmark);

    myBookmark = FBReaderIntents.getBookmarkExtra(getIntent());
    if (myBookmark == null) {
        finish();/* w ww .j av a2s  . co  m*/
        return;
    }

    final DisplayMetrics dm = getResources().getDisplayMetrics();
    final int width = Math.min((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 500, dm),
            dm.widthPixels * 9 / 10);
    final int height = Math.min((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 350, dm),
            dm.heightPixels * 9 / 10);

    final TabHost tabHost = (TabHost) findViewById(R.id.edit_bookmark_tabhost);
    tabHost.setLayoutParams(new FrameLayout.LayoutParams(new ViewGroup.LayoutParams(width, height)));
    tabHost.setup();

    addTab(tabHost, "text", R.id.edit_bookmark_content_text);
    addTab(tabHost, "style", R.id.edit_bookmark_content_style);
    addTab(tabHost, "delete", R.id.edit_bookmark_content_delete);

    final ZLStringOption currentTabOption = new ZLStringOption("LookNFeel", "EditBookmark", "text");
    tabHost.setCurrentTabByTag(currentTabOption.getValue());
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        public void onTabChanged(String tag) {
            if (!"delete".equals(tag)) {
                currentTabOption.setValue(tag);
            }
        }
    });

    final EditText editor = (EditText) findViewById(R.id.edit_bookmark_text);
    editor.setText(myBookmark.getText());
    final int len = editor.getText().length();
    editor.setSelection(len, len);

    final Button saveTextButton = (Button) findViewById(R.id.edit_bookmark_save_text_button);
    saveTextButton.setEnabled(false);
    saveTextButton.setText(myResource.getResource("saveText").getValue());
    saveTextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myCollection.bindToService(EditBookmarkActivity.this, new Runnable() {
                public void run() {
                    myBookmark.setText(editor.getText().toString());
                    myCollection.saveBookmark(myBookmark);
                    saveTextButton.setEnabled(false);
                }
            });
        }
    });
    editor.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence sequence, int start, int before, int count) {
            final String originalText = myBookmark.getText();
            saveTextButton.setEnabled(!originalText.equals(editor.getText().toString()));
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    final Button deleteButton = (Button) findViewById(R.id.edit_bookmark_delete_button);
    deleteButton.setText(myResource.getResource("deleteBookmark").getValue());
    deleteButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            myCollection.bindToService(EditBookmarkActivity.this, new Runnable() {
                public void run() {
                    myCollection.deleteBookmark(myBookmark);
                    finish();
                }
            });
        }
    });
}

From source file:com.cerema.cloud2.ui.dialog.RenameFileDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);

    // Inflate the layout for the dialog
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.edit_box_dialog, null);

    // Setup layout 
    String currentName = mTargetFile.getFileName();
    EditText inputText = ((EditText) v.findViewById(R.id.user_input));
    inputText.setText(currentName);/*from   w  w w  . ja  v  a  2 s . com*/
    int selectionStart = 0;
    int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf(".");
    int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
    if (selectionStart >= 0 && selectionEnd >= 0) {
        inputText.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd));
    }
    inputText.requestFocus();

    // Build the dialog  
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(v).setPositiveButton(R.string.common_ok, this)
            .setNegativeButton(R.string.common_cancel, this).setTitle(R.string.rename_dialog_title);
    Dialog d = builder.create();
    d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return d;
}

From source file:com.synox.android.ui.dialog.RenameFileDialogFragment.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);

    // Inflate the layout for the dialog
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View v = inflater.inflate(R.layout.edit_box_dialog, null);

    // Setup layout 
    String currentName = mTargetFile.getFileName();
    EditText inputText = ((EditText) v.findViewById(R.id.user_input));
    inputText.setText(currentName);/*from   w  ww  .ja va  2s.  c  om*/
    int selectionStart = 0;
    int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf(".");
    int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
    if (selectionStart >= 0 && selectionEnd >= 0) {
        inputText.setSelection(Math.min(selectionStart, selectionEnd), Math.max(selectionStart, selectionEnd));
    }
    inputText.requestFocus();

    // Build the dialog  
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.synox_AlertDialog);
    builder.setView(v).setPositiveButton(R.string.common_ok, this)
            .setNegativeButton(R.string.common_cancel, this).setTitle(R.string.rename_dialog_title);
    Dialog d = builder.create();
    d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return d;
}

From source file:ren.qinc.markdowneditors.view.FolderManagerFragment.java

/**
 * Re name.//from ww  w. ja v a 2s  .c  o  m
 * ????
 */
private void rename() {
    FileBean selectBean = mPresenter.getSelectBean();
    if (selectBean == null) {
        return;
    }

    //????
    View rootView = LayoutInflater.from(mContext).inflate(R.layout.view_common_input_view, null);

    AlertDialog dialog = new AlertDialog.Builder(mContext).setTitle("???").setView(rootView).show();

    TextInputLayout textInputLayout = (TextInputLayout) rootView.findViewById(R.id.inputHint);
    EditText text = (EditText) rootView.findViewById(R.id.text);
    text.setText(selectBean.name);
    text.setSelection(0, selectBean.isDirectory ? selectBean.name.length() : selectBean.name.lastIndexOf("."));
    textInputLayout.setHint("" + (selectBean.isDirectory ? "??" : "??"));
    rootView.findViewById(R.id.sure).setOnClickListener(v -> {
        String result = text.getText().toString().trim();
        if (!selectBean.isDirectory && !result.endsWith(".md") && !result.endsWith(".markdown")
                && !result.endsWith(".markd")) {
            textInputLayout.setError("???md|markdown|markd");
            return;
        }
        if (Check.isEmpty(result)) {
            textInputLayout.setError("?");
            return;
        }
        if (!selectBean.isDirectory && mPresenter.fileIsExists(result)) {
            textInputLayout.setError("?");
            return;
        }
        if (selectBean.isDirectory && mPresenter.createFoloderIsExists(result)) {
            textInputLayout.setError("?");
            return;
        }

        if (!mPresenter.rename(selectBean, result)) {
            textInputLayout.setError("???");
            return;
        }

        textInputLayout.setErrorEnabled(false);

        if (mActionMode != null) {
            mActionMode.finish();
        }
        dialog.dismiss();
    });

    rootView.findViewById(R.id.cancel).setOnClickListener(v -> {
        dialog.dismiss();
    });

    dialog.show();

}

From source file:com.sentaroh.android.Utilities.Dialog.SafFileSelectDialogFragment.java

private void restoreViewContents() {
    if (mDebugEnable)
        Log.v(APPLICATION_TAG, "restoreViewContents mSavedViewContentsValue=" + mSavedViewContentsValue);
    if (mSavedViewContentsValue == null)
        return;/*from ww w. j  a v a 2 s.  c om*/
    Handler hndl = new Handler();
    hndl.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mCreateDirDialog != null) {
                Button btnCreate = (Button) mDialog.findViewById(R.id.file_select_edit_dlg_create_btn);
                btnCreate.performClick();
                final EditText etDir = (EditText) mCreateDirDialog.findViewById(R.id.single_item_input_dir);
                etDir.setText(mSavedViewContentsValue.createDialogEditText);
                etDir.setSelection(mSavedViewContentsValue.createDialogEditTextSelStart,
                        mSavedViewContentsValue.createDialogEditTextSelEnd);
            }
        }
    }, 10);

    if (mSavedViewContentsValue.mainDailogListItems != null) {
        mTreeFilelistAdapter.setDataList(mSavedViewContentsValue.mainDailogListItems);
        mTreeFileListView.setSelectionFromTop(mSavedViewContentsValue.mainDailogListViewPos[0],
                mSavedViewContentsValue.mainDailogListViewPos[1]);
    }
    resetSavedViewContents();
}

From source file:info.staticfree.android.units.Units.java

private void swapInputs(EditText focused, EditText unfocused) {

    final Editable e = wantEditText.getText();
    int start = 0, end = 0;
    if (focused != null) {
        start = focused.getSelectionStart();
        end = focused.getSelectionEnd();
    }/* w w w  .j a v  a 2 s  . c o m*/

    wantEditText.setText(haveEditText.getText());
    haveEditText.setText(e);
    if (unfocused != null) {
        unfocused.requestFocus();
        unfocused.setSelection(start, end);
    }
}

From source file:com.sentaroh.android.Utilities.Dialog.FileSelectDialogFragment.java

private void restoreViewContents() {
    if (mDebugEnable)
        Log.v(APPLICATION_TAG, "restoreViewContents mSavedViewContentsValue=" + mSavedViewContentsValue);
    if (mSavedViewContentsValue == null)
        return;/*from www. j  ava2 s.  c  o m*/
    Handler hndl = new Handler();
    hndl.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mCreateDirDialog != null) {
                Button btnCreate = (Button) mDialog.findViewById(R.id.file_select_edit_dlg_create_btn);
                btnCreate.performClick();
                final EditText etDir = (EditText) mCreateDirDialog.findViewById(R.id.single_item_input_dir);
                etDir.setText(mSavedViewContentsValue.createDialogEditText);
                etDir.setSelection(mSavedViewContentsValue.createDialogEditTextSelStart,
                        mSavedViewContentsValue.createDialogEditTextSelEnd);
            }
        }
    }, 10);
    if (mSavedViewContentsValue.mainDialogSpinnerPos != -1)
        mLocalMountPointSpinner.setSelection(mSavedViewContentsValue.mainDialogSpinnerPos);

    if (mSavedViewContentsValue.mainDailogListItems != null) {
        mTreeFilelistAdapter.setDataList(mSavedViewContentsValue.mainDailogListItems);
        mTreeFileListView.setSelectionFromTop(mSavedViewContentsValue.mainDailogListViewPos[0],
                mSavedViewContentsValue.mainDailogListViewPos[1]);
    }
    resetSavedViewContents();
}