Put constant text inside EditText which should be non-editable - Android Put constant text inside EditText which should be non-editable - Android android android

Put constant text inside EditText which should be non-editable - Android


Did u try this method?

final EditText edt = (EditText) findViewById(R.id.editText1);edt.setText("http://");Selection.setSelection(edt.getText(), edt.getText().length());edt.addTextChangedListener(new TextWatcher() {        @Override        public void onTextChanged(CharSequence s, int start, int before, int count) {            // TODO Auto-generated method stub        }        @Override        public void beforeTextChanged(CharSequence s, int start, int count,                int after) {            // TODO Auto-generated method stub        }        @Override        public void afterTextChanged(Editable s) {            if(!s.toString().startsWith("http://")){                edt.setText("http://");                Selection.setSelection(edt.getText(), edt.getText().length());            }        }    });


As of version 1.2.0-alpha01 of material design library, prefix and suffix is supported for text fields:

<com.google.android.material.textfield.TextInputLayout        app:prefixText="Price: "        app:prefixTextAppearance="..."        app:prefixTextColor="..."        app:suffixText="Dollar"        app:suffixTextColor="..."        app:suffixTextAppearance="...">    <com.google.android.material.textfield.TextInputEditText .../></com.google.android.material.textfield.TextInputLayout>

The only downside in my opinion is that the suffix is fixed at the end of the text field and there is no option to make it flow with the input text. You can vote on this issue for that.


That's how you can actually do it with an InputFilter:

final String prefix = "http://"editText.setText(prefix);editText.setFilters(new InputFilter[] {    new InputFilter() {      @Override      public CharSequence filter(final CharSequence source, final int start,          final int end, final Spanned dest, final int dstart, final int dend) {        final int newStart = Math.max(prefix.length(), dstart);        final int newEnd = Math.max(prefix.length(), dend);        if (newStart != dstart || newEnd != dend) {          final SpannableStringBuilder builder = new SpannableStringBuilder(dest);          builder.replace(newStart, newEnd, source);          if (source instanceof Spanned) {            TextUtils.copySpansFrom(                (Spanned) source, 0, source.length(), null, builder, newStart);          }          Selection.setSelection(builder, newStart + source.length());          return builder;        } else {          return null;        }      }    }});

If you also want the prefix to be not selectable you can add the following code.

final SpanWatcher watcher = new SpanWatcher() {  @Override  public void onSpanAdded(final Spannable text, final Object what,      final int start, final int end) {    // Nothing here.  }  @Override  public void onSpanRemoved(final Spannable text, final Object what,       final int start, final int end) {    // Nothing here.  }  @Override  public void onSpanChanged(final Spannable text, final Object what,       final int ostart, final int oend, final int nstart, final int nend) {    if (what == Selection.SELECTION_START) {      if (nstart < prefix.length()) {        final int end = Math.max(prefix.length(), Selection.getSelectionEnd(text));        Selection.setSelection(text, prefix.length(), end);      }    } else if (what == Selection.SELECTION_END) {      final int start = Math.max(prefix.length(), Selection.getSelectionEnd(text));      final int end = Math.max(start, nstart);      if (end != nstart) {        Selection.setSelection(text, start, end);      }    }  }};editText.getText().setSpan(watcher, 0, 0, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);