Can we have uneditable text in edittext

I am using an EditText. Is it possible to have a part of text uneditable and the rest editable in the same EditText?

  • What is use of android:supportsRtl=“true” in AndroidManifest xml file
  • Android Studio Warning: Using incompatible plugins for the annotation processing
  • How to access data/data folder in Android device?
  • How to get gravity 'bottom' working on a drawable in xml
  • Android how do I set a percentage padding/margin so EditText has 10% margin on either side?
  • Swiping Images in ViewFlipper or ViewPager
  • Building phone gap app for Android failing
  • TextView onClick() not working
  • How to disable WIFI Auto-connect in Android
  • getWidth and getHeight are returning a zero
  • Android - Swipe to delete RecyclerView
  • adb error on Android SDK. Using Linux Ubuntu 64 bit 12.10
  • 2 Solutions collect form web for “Can we have uneditable text in edittext”

    You could use

    editText.setFocusable(false);

    or

    editText.setEnabled(false);

    although disabling the EditText does currently not ignore input from the on-screen keyboard (I think that’s a bug).

    Depending on the application it might be better to use an InputFilter that rejects all changes:

    editText.setFilters(new InputFilter[] {
        new InputFilter() {
            public CharSequence filter(CharSequence src, int start,
                int end, Spanned dst, int dstart, int dend) {
                return src.length() < 1 ? dst.subSequence(dstart, dend) : "";
            }
        }
    });

    Also see this question.

    You can implement a TextChangedListener where you make sure those parts of your text wont get deleted/overwritten.

    class TextChangedListener implements TextWatcher {
        public void afterTextChanged(Editable s) {
                    makeSureNothingIsDeleted();
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
    }
    

        TextChangedListener tcl = new TextChangedListener();
        my_editable.addTextChangedListener(tcl);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.