38

I have a EditText in which the user should not be able to provide input. So I tried disabling it,by

edittext.setEnabled(false);
edittext.setClickable(false);

But still when I press the "next" button in the softKeyboard from some other EditText it directs me to the one which should not be editable and I am able to insert values into it. How to avoid this?

iDroid
  • 10,403
  • 1
  • 19
  • 27
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • where do you use these commands? in which event? – olix20 Jul 18 '11 at 09:39
  • have you checked http://stackoverflow.com/questions/910973/why-can-i-type-into-a-disabled-edittext – olix20 Jul 18 '11 at 09:51
  • your que is similar to this que http://stackoverflow.com/questions/660151/how-to-replicate-androideditable-false-in-code – Avi Kumar Jul 18 '11 at 10:02
  • 1
    The whole point of the **EditText** is to allow the user to **edit** its contents. Is there any reason you're not using TextView? – Marmoy Jul 18 '11 at 10:19

11 Answers11

39

This works fine; just set focusable property of your edittext to "false" and you are done.

<EditText
        android:id="@+id/EditTextInput"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:gravity="right"
        android:cursorVisible="true">
    </EditText>
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Khurram Shehzad
  • 1,010
  • 12
  • 16
  • 28
    it is not an acceptable answer why because if we long click on EditText we are able to paste new data using this solution. and the correct solution is EditTextInput.setKeyListener(null); – Venkat May 06 '15 at 07:00
  • @Venkat I agree that the answer shouldn't have been accepted. We just experienced a problem where `isFocusable = false` still allowed to long-tap and copy-paste anything in the input that was supposed to be disabled. Your answer actually does what was expected. – NeverwinterMoon Mar 22 '17 at 11:38
36

don't messed up with your self. just simple to work use below one

First, save the EditText's key listener:

KeyListener mKeyListener = yourTextView.getKeyListener();
yourTextView.setKeyListener(null);

Then if you want to enable editing again, assign the saved listener back:

yourTextView.setKeyListener(mKeyListener);

source

Community
  • 1
  • 1
codercat
  • 22,873
  • 9
  • 61
  • 85
18
yourEditText.setKeyListener(null);

No need to remove focus and setting clickable false.

Just remove key listener and you are done.

Also tested with Android 6.0

Azim Ansari
  • 1,378
  • 11
  • 20
7

Ok. I did something like this and got the result.

    edittext.setEnabled(false);
    edittext.setGravity(Gravity.CENTER);
    edittext.setClickable(false);
    edittext.setFocusable(false);
    edittext.setFilters(new InputFilter[] { new textChange() });
    edittext.setInputType(0);

       class textChange implements InputFilter {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {

        return dest.subSequence(dstart, dend);
    }
}
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
5
android:focusable="false"
android:longClickable="false"

Adding these two fields to your EditText will disables the focus and also disables the copy pasting data into your EditText field.

skaveesh
  • 369
  • 6
  • 9
4

Try adding

edittext.setFocusable(false); edittest.setFocusableInTouchMode(false);

PravinCG
  • 7,688
  • 3
  • 30
  • 55
1

Take a look at this

editText.setEnabled(false); and editText.setFocusable(false); will achieve.

einverne
  • 6,454
  • 6
  • 45
  • 91
0

In your EditText Xml Code . Paste below line.

android:focusable="false"
Piash Sarker
  • 103
  • 1
  • 6
0
android:focusable="false" 

worked for me.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – J. Chomel Jan 27 '17 at 10:19
0

Use imeOptions attribute to change that button in softKeyboard to done

android:imeOptions="actionDone"

add it to EditText just previous to the one you want to disable.

0

It might be somebody helpful. I've found one implementation:

@Override
    public boolean onTouch(View v, MotionEvent event) {
        //for edit text
        return locked;
    }

where locked is locked state.

of course, i have to use editText.setOnTouchListener(<listener>);

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194