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:com.google.dotorg.crisisresponse.translationcards.RecordingActivity.java

private void moveToLabelStep() {
    setContentView(R.layout.recording_label);
    recycleBitmap();//from  ww w  .j  a v  a  2  s. c om
    currentBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.recording_label_image);
    currentBitmapView = (ImageView) findViewById(R.id.recording_label_image);
    currentBitmapView.setImageBitmap(currentBitmap);
    final EditText labelField = (EditText) findViewById(R.id.recording_label_field);
    if (label != null) {
        labelField.setText(label);
        labelField.setTextColor(Color.BLACK);
        labelField.setSelection(label.length());
        setLabelNextButtonEnabled(true);
    }
    if (inEditMode) {
        ImageView deleteButton = (ImageView) findViewById(R.id.recording_label_delete_image);
        deleteButton.setVisibility(View.VISIBLE);
        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DbManager dbm = new DbManager(RecordingActivity.this);
                dbm.deleteTranslation(translationId);
                if (!isAsset) {
                    File oldFile = new File(filename);
                    oldFile.delete();
                    if (!savedIsAsset && (savedFilename != null) && !savedFilename.equals(filename)) {
                        oldFile = new File(savedFilename);
                        oldFile.delete();
                    }
                }
                setResult(RESULT_OK);
                finish();
            }
        });
        findViewById(R.id.recording_label_step_marker).setVisibility(View.GONE);
    }
    labelField.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus
                    && labelField.getText().toString().equals(getString(R.string.recording_label_hint_text))) {
                labelField.setText("");
                labelField.setTextColor(Color.BLACK);
            } else if (!hasFocus && labelField.getText().toString().equals("")) {
                labelField.setText(getString(R.string.recording_label_hint_text));
                labelField.setTextColor(getResources().getColor(R.color.borderColor));
            }
            if (hasFocus) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                        Context.INPUT_METHOD_SERVICE);
                inputMethodManager.toggleSoftInputFromWindow(labelField.getApplicationWindowToken(),
                        InputMethodManager.SHOW_FORCED, 0);
            }
        }
    });
    labelField.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // Do nothing here.
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0 && !s.equals(getString(R.string.recording_label_hint_text))) {
                setLabelNextButtonEnabled(true);
            } else {
                setLabelNextButtonEnabled(false);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {
            // Do nothing here.
        }
    });
    View nextButton = (View) findViewById(R.id.recording_label_next);
    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            label = labelField.getText().toString();
            if (label.length() == 0 || label.equals(getString(R.string.recording_label_hint_text))) {
                return;
            }
            moveToAudioStep();
        }
    });
    stepHistory.push(Step.LABEL);
}

From source file:de.gebatzens.sia.dialog.LoginDialog.java

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

    boolean hideSid = getArguments().getBoolean("hideSid");
    String sid = getArguments().getString("sid");
    boolean auth = getArguments().getBoolean("auth");
    String user = getArguments().getString("user");
    final AlertDialog dialog = (AlertDialog) getDialog();

    ((SetupActivity) getActivity()).currentLoginDialog = dialog;

    if (auth) {/*from w  ww.j  a  v a2 s . c o m*/
        final EditText passwordInput = (EditText) dialog.findViewById(R.id.passwordInput);
        final CheckBox passwordToggle = (CheckBox) dialog.findViewById(R.id.passwordToggle);
        passwordToggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    passwordInput.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    passwordInput.setSelection(passwordInput.getText().length());
                } else {
                    passwordInput.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    passwordInput.setSelection(passwordInput.getText().length());
                }
            }
        });

        ((EditText) dialog.findViewById(R.id.usernameInput)).setText(user);
    } else {
        dialog.findViewById(R.id.passwordInput).setVisibility(View.GONE);
        dialog.findViewById(R.id.passwordToggle).setVisibility(View.GONE);
        dialog.findViewById(R.id.usernameInput).setVisibility(View.GONE);
    }

    if (hideSid) {
        dialog.findViewById(R.id.sidInput).setVisibility(View.GONE);
    } else {
        ((EditText) dialog.findViewById(R.id.sidInput)).setText(sid);
    }

    TextView acceptTermsLink = (TextView) dialog.findViewById(R.id.acceptTermsLink);
    acceptTermsLink.setPaintFlags(acceptTermsLink.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

    acceptTermsLink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View widget) {
            Intent i = new Intent(activity, TextActivity.class);
            i.putExtra("title", R.string.terms_title);
            i.putExtra("text", R.array.terms);
            activity.startActivity(i);
        }
    });

    final CheckBox acceptTerms = (CheckBox) dialog.findViewById(R.id.acceptTerms);

    acceptTerms.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(isChecked);
        }
    });

    dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(acceptTerms.isChecked());

}

From source file:ca.mymenuapp.ui.debug.DebugAppContainer.java

private void showCustomEndpointDialog(final int originalSelection, String defaultUrl) {
    View view = LayoutInflater.from(app).inflate(R.layout.debug_drawer_network_endpoint, null);
    final EditText url = findById(view, R.id.debug_drawer_network_endpoint_url);
    url.setText(defaultUrl);/*from ww w.j av a2 s.  c  o m*/
    url.setSelection(url.length());

    new AlertDialog.Builder(activity) //
            .setTitle("Set Network Endpoint").setView(view)
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    endpointView.setSelection(originalSelection);
                    dialog.cancel();
                }
            }).setPositiveButton("Use", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    String theUrl = url.getText().toString();
                    if (!Strings.isBlank(theUrl)) {
                        setEndpointAndRelaunch(theUrl);
                    } else {
                        endpointView.setSelection(originalSelection);
                    }
                }
            }).setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialogInterface) {
                    endpointView.setSelection(originalSelection);
                }
            }).show();
}

From source file:com.waz.zclient.pages.main.profile.preferences.dialogs.ChangeEmailPreferenceDialogFragment.java

@SuppressLint("InflateParams")
@Override/*from w ww. j ava2 s.co  m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    final LayoutInflater inflater = LayoutInflater.from(getActivity());
    final View view = inflater.inflate(R.layout.preference_dialog_change_email, null);

    inputLayout = ViewUtils.getView(view, R.id.til__preferences__email);
    final EditText editText = ViewUtils.getView(view, R.id.acet__preferences__email);
    editText.requestFocus();
    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                handleInput();
                return true;
            } else {
                return false;
            }
        }
    });
    final String existingEmail = getArguments().getString(ARG_EMAIL, "");
    editText.setText(existingEmail);
    editText.setSelection(editText.length());

    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
            .setTitle(R.string.pref__account_action__dialog__change_email__title).setView(view)
            .setPositiveButton(android.R.string.ok, null).setNegativeButton(android.R.string.cancel, null)
            .create();
    alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    return alertDialog;
}

From source file:com.numix.calculator.EventListener.java

@Override
public boolean onLongClick(View view) {
    switch (view.getId()) {
    case R.id.del:
        mHandler.onClear();//from   w  w w  . j a v  a2 s  .c  om
        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_LEFT);
            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:sssemil.com.hostsaway.ui.RedirectionListFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info//www . j  a  v  a  2 s . 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;

    TextView hostnameTextView = (TextView) v.findViewWithTag("hostname_" + position);
    TextView ipTextView = (TextView) v.findViewWithTag("ip_" + 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_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);

    // set text from list
    hostnameEditText.setText(hostnameTextView.getText());
    ipEditText.setText(ipTextView.getText());

    // move cursor to end of EditText
    Editable hostnameEditContent = hostnameEditText.getText();
    hostnameEditText.setSelection(hostnameEditContent.length());
    Editable ipEditContent = ipEditText.getText();
    ipEditText.setSelection(ipEditContent.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 hostname = hostnameEditText.getText().toString();
                    String ip = ipEditText.getText().toString();

                    if (RegexUtils.isValidHostname(hostname)) {
                        if (RegexUtils.isValidIP(ip)) {
                            ProviderHelper.updateRedirectionListItemHostnameAndIp(mActivity, mCurrentRowId,
                                    hostname, ip);
                        } else {
                            AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                            alertDialog.setTitle(R.string.no_ip_title);
                            alertDialog.setMessage(getString(sssemil.com.hostsaway.R.string.no_ip));
                            alertDialog.setButton(getString(R.string.button_close),
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dlg, int sum) {
                                            dlg.dismiss();
                                        }
                                    });
                            alertDialog.show();
                        }
                    } 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(sssemil.com.hostsaway.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.adaway.ui.RedirectionListFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info/*from   w  w w  .  j  a  v  a  2s.  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;

    TextView hostnameTextView = (TextView) v.findViewWithTag("hostname_" + position);
    TextView ipTextView = (TextView) v.findViewWithTag("ip_" + 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_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);

    // set text from list
    hostnameEditText.setText(hostnameTextView.getText());
    ipEditText.setText(ipTextView.getText());

    // move cursor to end of EditText
    Editable hostnameEditContent = hostnameEditText.getText();
    hostnameEditText.setSelection(hostnameEditContent.length());
    Editable ipEditContent = ipEditText.getText();
    ipEditText.setSelection(ipEditContent.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 hostname = hostnameEditText.getText().toString();
                    String ip = ipEditText.getText().toString();

                    if (RegexUtils.isValidHostname(hostname)) {
                        if (RegexUtils.isValidIP(ip)) {
                            ProviderHelper.updateRedirectionListItemHostnameAndIp(mActivity, mCurrentRowId,
                                    hostname, ip);
                        } else {
                            AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                            alertDialog.setTitle(R.string.no_ip_title);
                            alertDialog.setMessage(getString(org.adaway.R.string.no_ip));
                            alertDialog.setButton(getString(R.string.button_close),
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dlg, int sum) {
                                            dlg.dismiss();
                                        }
                                    });
                            alertDialog.show();
                        }
                    } 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(org.adaway.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.RedirectionListFragment.java

/**
 * Edit entry based on selection in context menu
 *
 * @param info/*from  w w  w  . ja  v a  2 s .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;

    TextView hostnameTextView = (TextView) v.findViewWithTag("hostname_" + position);
    TextView ipTextView = (TextView) v.findViewWithTag("ip_" + 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_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);

    // set text from list
    hostnameEditText.setText(hostnameTextView.getText());
    ipEditText.setText(ipTextView.getText());

    // move cursor to end of EditText
    Editable hostnameEditContent = hostnameEditText.getText();
    hostnameEditText.setSelection(hostnameEditContent.length());
    Editable ipEditContent = ipEditText.getText();
    ipEditText.setSelection(ipEditContent.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 hostname = hostnameEditText.getText().toString();
                    String ip = ipEditText.getText().toString();

                    if (org.adawaycn.util.RegexUtils.isValidHostname(hostname)) {
                        if (org.adawaycn.util.RegexUtils.isValidIP(ip)) {
                            org.adawaycn.provider.ProviderHelper.updateRedirectionListItemHostnameAndIp(
                                    mActivity, mCurrentRowId, hostname, ip);
                        } else {
                            AlertDialog alertDialog = new AlertDialog.Builder(mActivity).create();
                            alertDialog.setIcon(android.R.drawable.ic_dialog_alert);
                            alertDialog.setTitle(R.string.no_ip_title);
                            alertDialog.setMessage(getString(R.string.no_ip));
                            alertDialog.setButton(getString(R.string.button_close),
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dlg, int sum) {
                                            dlg.dismiss();
                                        }
                                    });
                            alertDialog.show();
                        }
                    } 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.wubydax.dbeditor.TableValuesFragment.java

private void showDialog(final String key, final String value, final int position) {
    @SuppressLint("InflateParams")
    View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_layout, null, false);
    TextView keyText = (TextView) view.findViewById(R.id.textKey);
    final EditText editText = (EditText) view.findViewById(R.id.valueEditText);
    final ScrollView scroll = (ScrollView) view.findViewById(R.id.ScrollView1);
    if (value.matches("\\d+(?:\\.\\d+)?")) {
        editText.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        editText.setSingleLine(true);//  w ww.  j  a v  a 2s  .  c om
    } else {
        editText.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        editText.setHint(getResources().getString(R.string.enter_string));
    }
    keyText.setText(key);
    editText.setText(value);
    editText.setSelection(editText.getText().length());
    scroll.post(new Runnable() {
        @Override
        public void run() {
            scroll.fullScroll(View.FOCUS_DOWN);
        }
    });
    new AlertDialog.Builder(getActivity()).setTitle(getResources().getString(R.string.change_value))
            .setView(view).setNegativeButton(android.R.string.cancel, null)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String newValue = editText.getText().toString();
                    boolean isGranted = Settings.System.canWrite(getActivity());
                    if (!isGranted) {
                        Intent grantPermission = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                        startActivity(grantPermission);
                    } else {
                        switch (mTableName) {
                        case "system":
                            Settings.System.putString(getActivity().getContentResolver(), key, newValue);
                            break;
                        case "global":
                            Settings.Global.putString(getActivity().getContentResolver(), key, newValue);
                            break;
                        case "secure":
                            Settings.Secure.putString(getActivity().getContentResolver(), key, newValue);
                            break;
                        }
                        mList.get(position).value = newValue;
                    }
                    mRecyclerView.getAdapter().notifyDataSetChanged();
                }
            }).show();
}

From source file:free.yhc.feeder.ChannelListActivity.java

private void onOpt_addChannel_url(final View anchor) {
    // Set action for dialog.
    final EditTextDialogAction action = new EditTextDialogAction() {
        @Override/*from  w  w w .  j av a  2  s.  c om*/
        public void prepare(Dialog dialog, EditText edit) {
            // start edit box with 'http://'
            final String prefix = "http://";
            edit.setText(prefix);
            edit.setSelection(prefix.length());
        }

        @Override
        public void onOk(Dialog dialog, EditText edit) {
            String url = edit.getText().toString();
            if (!url.matches("http\\:\\/\\/\\s*")) {
                addChannel(url, null);
                mUr.storeUsageReport("URL : " + url + "\n");
            }
        }
    };
    UiHelper.buildOneLineEditTextDialog(this, R.string.channel_url, action).show();
}