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 index) 

Source Link

Document

Convenience for Selection#setSelection(Spannable,int) .

Usage

From source file:org.adawaycn.ui.WhitelistFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info/*from   ww w.jav a2s.c o m*/
 */
private void menuEditEntry(AdapterContextMenuInfo info) {
    mCurrentRowId = info.id; // set global RowId to row id from cursor to use inside save button
    int position = info.position;
    View v = info.targetView;

    CheckBox cBox = (CheckBox) v.findViewWithTag(position);

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_edit_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_whitelist_hostname_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_hostname);
    inputEditText.setText(cBox.getText());

    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_save),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();

                    if (RegexUtils.isValidWhitelistHostname(input)) {
                        ProviderHelper.updateWhitelistItemHostname(mActivity, mCurrentRowId, input);
                    } else {
                        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                        alertDialog.setTitle(R.string.no_hostname_title);
                        alertDialog.setMessage(getString(R.string.no_hostname));
                        alertDialog.setButton(getString(R.string.button_close),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sum) {
                                        dlg.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:org.adawaycn.ui.BlacklistFragment.java

/**
 * Edit entry based on selection in context menu
 * /*from  ww w  . j ava2  s .  c om*/
 * @param info
 */
private void menuEditEntry(AdapterContextMenuInfo info) {
    mCurrentRowId = info.id; // set global RowId to row id from cursor to use inside save button
    int position = info.position;
    View v = info.targetView;

    CheckBox cBox = (CheckBox) v.findViewWithTag(position);

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_edit_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_hostname_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_hostname);
    inputEditText.setText(cBox.getText());

    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_save),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();

                    if (org.adawaycn.util.RegexUtils.isValidHostname(input)) {
                        org.adawaycn.provider.ProviderHelper.updateBlacklistItemHostname(mActivity,
                                mCurrentRowId, input);
                    } else {
                        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                        alertDialog.setTitle(R.string.no_hostname_title);
                        alertDialog.setMessage(getString(R.string.no_hostname));
                        alertDialog.setButton(getString(R.string.button_close),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sum) {
                                        dlg.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:com.money.manager.ex.fragment.PayeeListFragment.java

private void showDialogEditPayeeName(final SQLTypeTransaction type, final int payeeId, final String payeeName) {
    View viewDialog = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_new_edit_payee, null);
    final EditText edtPayeeName = (EditText) viewDialog.findViewById(R.id.editTextPayeeName);

    edtPayeeName.setText(payeeName);//  ww  w  .  j a  va 2  s .c  o  m
    if (!TextUtils.isEmpty(payeeName)) {
        edtPayeeName.setSelection(payeeName.length());
    }

    new AlertDialogWrapper(getContext()).setView(viewDialog)
            .setIcon(FontIconDrawable.inflate(getContext(), R.xml.ic_user)).setTitle(R.string.edit_payeeName)
            .setPositiveButton(android.R.string.ok, new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    // take payee name from the input field.
                    String name = edtPayeeName.getText().toString();

                    PayeeService service = new PayeeService(mContext);

                    // check if action in update or insert
                    switch (type) {
                    case INSERT:
                        Payee payee = service.createNew(name);
                        if (payee != null) {
                            // Created a new payee. But only if picking a payee for another activity.
                            if (mAction.equalsIgnoreCase(Intent.ACTION_PICK)) {
                                // Select it and close.
                                sendResultToActivity(payee.getId(), name);
                                return;
                            }
                        } else {
                            // error inserting.
                            Toast.makeText(mContext, R.string.db_insert_failed, Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case UPDATE:
                        int updateResult = service.update(payeeId, name);
                        if (updateResult <= 0) {
                            Toast.makeText(mContext, R.string.db_update_failed, Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case DELETE:
                        break;
                    default:
                        break;
                    }
                    // restart loader
                    restartLoader();
                }
            }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            }).create().show();
}

From source file:org.adaway.ui.RedirectionListFragment.java

/**
 * Add Entry Menu Action/*  w  ww.  j a v  a2  s. c o m*/
 */
public void menuAddEntry() {
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_add_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_redirection_dialog, null);
    final EditText hostnameEditText = (EditText) dialogView.findViewById(R.id.list_dialog_hostname);
    final EditText ipEditText = (EditText) dialogView.findViewById(R.id.list_dialog_ip);

    // move cursor to end of EditText
    Editable hostnameEditContent = hostnameEditText.getText();
    hostnameEditText.setSelection(hostnameEditContent.length());

    // move cursor to end of EditText
    Editable ipEditContent = ipEditText.getText();
    ipEditText.setSelection(ipEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_add),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String hostname = hostnameEditText.getText().toString();
                    String ip = ipEditText.getText().toString();

                    addEntry(hostname, ip);
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:org.adaway.ui.HostsSourcesFragment.java

/**
 * Add Entry Menu Action//from   w ww .  j  a v  a2s.com
 */
public void menuAddEntry() {
    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_add_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_url_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_url);
    // set EditText
    inputEditText.setText(getString(R.string.hosts_add_dialog_input));
    inputEditText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_add),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();
                    addEntry(input);
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:sssemil.com.hostsaway.ui.HostsSourcesFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info/* w  w w .  j  a va 2 s.com*/
 */
private void menuEditEntry(AdapterContextMenuInfo info) {
    mCurrentRowId = info.id; // set global RowId to row id from cursor to use inside save button
    int position = info.position;
    View v = info.targetView;

    TextView urlTextView = (TextView) v.findViewWithTag("url_" + position);

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_edit_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_url_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_url);
    // set text from list
    inputEditText.setText(urlTextView.getText());
    inputEditText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_save),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();

                    if (RegexUtils.isValidUrl(input)) {
                        // update in db
                        ProviderHelper.updateHostsSourceUrl(mActivity, mCurrentRowId, input);
                    } else {
                        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                        alertDialog.setTitle(R.string.no_url_title);
                        alertDialog.setMessage(getString(sssemil.com.hostsaway.R.string.no_url));
                        alertDialog.setButton(getString(R.string.button_close),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sum) {
                                        dlg.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:org.adaway.ui.HostsSourcesFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info//  ww  w.j  a  va 2  s.com
 */
private void menuEditEntry(AdapterContextMenuInfo info) {
    mCurrentRowId = info.id; // set global RowId to row id from cursor to use inside save button
    int position = info.position;
    View v = info.targetView;

    TextView urlTextView = (TextView) v.findViewWithTag("url_" + position);

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_edit_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_url_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_url);
    // set text from list
    inputEditText.setText(urlTextView.getText());
    inputEditText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_save),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();

                    if (RegexUtils.isValidUrl(input)) {
                        // update in db
                        ProviderHelper.updateHostsSourceUrl(mActivity, mCurrentRowId, input);
                    } else {
                        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                        alertDialog.setTitle(R.string.no_url_title);
                        alertDialog.setMessage(getString(org.adaway.R.string.no_url));
                        alertDialog.setButton(getString(R.string.button_close),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sum) {
                                        dlg.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:org.adawaycn.ui.HostsSourcesFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info/*ww w .  j  a v a 2  s.  c  om*/
 */
private void menuEditEntry(AdapterContextMenuInfo info) {
    mCurrentRowId = info.id; // set global RowId to row id from cursor to use inside save button
    int position = info.position;
    View v = info.targetView;

    TextView urlTextView = (TextView) v.findViewWithTag("url_" + position);

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
    builder.setCancelable(true);
    builder.setTitle(getString(R.string.checkbox_list_edit_dialog_title));

    // build view from layout
    LayoutInflater factory = LayoutInflater.from(mActivity);
    final View dialogView = factory.inflate(R.layout.lists_url_dialog, null);
    final EditText inputEditText = (EditText) dialogView.findViewById(R.id.list_dialog_url);
    // set text from list
    inputEditText.setText(urlTextView.getText());
    inputEditText.setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    // move cursor to end of EditText
    Editable inputEditContent = inputEditText.getText();
    inputEditText.setSelection(inputEditContent.length());

    builder.setView(dialogView);

    builder.setPositiveButton(getResources().getString(R.string.button_save),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                    String input = inputEditText.getText().toString();

                    if (RegexUtils.isValidUrl(input)) {
                        // update in db
                        ProviderHelper.updateHostsSourceUrl(mActivity, mCurrentRowId, input);
                    } else {
                        AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                        alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                        alertDialog.setTitle(R.string.no_url_title);
                        alertDialog.setMessage(getString(R.string.no_url));
                        alertDialog.setButton(getString(R.string.button_close),
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dlg, int sum) {
                                        dlg.dismiss();
                                    }
                                });
                        alertDialog.show();
                    }
                }
            });
    builder.setNegativeButton(getResources().getString(R.string.button_cancel),
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
    AlertDialog alert = builder.create();
    alert.show();
}

From source file:com.android.calculator2.EventListener.java

@Override
public boolean onLongClick(View view) {
    switch (view.getId()) {
    case R.id.del:
        mHandler.onClear();/* w  w  w .  ja  va2 s.c o  m*/
        return true;

    case R.id.next:
        // Handle back
        EditText active = mHandler.mDisplay.getActiveEditText();
        if (active.getSelectionStart() == 0) {
            View v = mHandler.mDisplay.getActiveEditText().focusSearch(View.FOCUS_BACKWARD);
            if (v != null)
                v.requestFocus();
            active = mHandler.mDisplay.getActiveEditText();
            active.setSelection(active.getText().length());
        } else {
            active.setSelection(active.getSelectionStart() - 1);
        }
        return true;
    }
    if (view.getTag() != null) {
        String text = (String) view.getTag();
        if (!text.isEmpty()) {
            Toast.makeText(mContext, text, Toast.LENGTH_SHORT).show();
            return true;
        }
    }
    if (view instanceof TextView && ((TextView) view).getHint() != null) {
        String text = ((TextView) view).getHint().toString();
        if (text.length() >= 2) {
            // Add paren after sin, cos, ln, etc. from buttons
            text += "(";
        }
        mHandler.insert(text);
        returnToBasic();
        return true;
    }
    return false;
}

From source file:org.mercycorps.translationcards.activity.RecordingActivity.java

private void fillPrepopulatedField(String fieldValue, EditText field, String hintText) {
    if (fieldValue != null && !fieldValue.isEmpty()) {
        field.setText(fieldValue);/* w ww .j  a  v a2 s . com*/
        field.setTextColor(Color.BLACK);
        field.setSelection(fieldValue.length());
        setLabelNextButtonEnabled(true);
    } else {
        field.setHint(hintText);
    }
}