Hesam
Hesam

Reputation: 53610

Disabling of EditText in Android

In my application, I have an EditText that the user only has Read access not Write access.

In code I set android:enabled="false".

Although the background of EditText changed to dark, when I click on it the keyboard pops up and I can change the text.

What should I set to disable EditText?

Upvotes: 247

Views: 395166

Answers (28)

Veniamin
Veniamin

Reputation: 864

The answers above don't really work in all cases, e.g. the Talkback mode.

I added an additional view on top of the EditText and assigned a click listener to it.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    ...
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/textInputLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/textInputEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </com.google.android.material.textfield.TextInputLayout>

            <View
                android:id="@+id/btnTextInputLayout"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:background="?selectableItemBackground"
                app:layout_constraintBottom_toBottomOf="@+id/textInputLayout"
                app:layout_constraintEnd_toEndOf="@+id/textInputLayout"
                app:layout_constraintStart_toStartOf="@+id/textInputLayout"
                app:layout_constraintTop_toTopOf="@+id/textInputLayout" />

    ...

</androidx.constraintlayout.widget.ConstraintLayout>

Helper functions:

    private fun makeOverlayEditTextEditable(
        editText: EditText,
        clickOverlayView: View,
        inputType: Int
    ) {
        enableEditTextInput(editText, inputType)
        clickOverlayView.isVisible = false
    }

    private fun makeOverlayEditTextClickable(
        editText: EditText,
        clickOverlayView: View,
        clickListener: () -> Unit
    ) {
        disableEditTextInput(editText)
        clickOverlayView.setOnClickListener {
            clickListener()
        }
        clickOverlayView.isVisible = true
    }
    @SuppressLint("ClickableViewAccessibility")
    private fun disableEditTextInput(editText: EditText) {
        editText.inputType = InputType.TYPE_NULL
        editText.isFocusable = false
        editText.isFocusableInTouchMode = false
        editText.isClickable = false
        editText.setOnTouchListener { _, _ -> true }
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun enableEditTextInput(editText: EditText, inputType: Int) {
        editText.inputType = inputType
        editText.isFocusable = true
        editText.isFocusableInTouchMode = true
        editText.isClickable = true
        editText.setOnTouchListener(null)
    }

Usage:

// Make EditText editable
makeOverlayEditTextEditable(
    editText = binding.textInputLayout,
    clickOverlayView = binding.btnTextInputLayout,
    inputType = InputType.TYPE_CLASS_TEXT
)

// Make EditText clickable
makeOverlayEditTextClickable(
    editText = binding.textInputLayout,
    clickOverlayView = binding.btnTextInputLayout,
) {
    // Handle click
}

If this pattern is used all across the application, it makes sense to abstract this logic in the custom view.

Upvotes: 0

Choletski
Choletski

Reputation: 7535

You can use this ext function in Kotlin:

fun EditText.disableInput() {
    isFocusable = false
    isEnabled = false
    isCursorVisible = false
    keyListener = null
    setBackgroundColor(Color.TRANSPARENT)
}

like so:

myEditText.disableInput()

Upvotes: 0

user14551921
user14551921

Reputation:

Using android:editable="false" is Depracted. Instead you'll need to Use android:focusable="false"

Upvotes: 4

Aminul Haque Aome
Aminul Haque Aome

Reputation: 2619

I use google newly released Material Design Library. In my case, it works when I use android:focusable="false" and android:cursorVisible="false"

<com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/to_time_input_layout"
                    app:endIconMode="custom"
                    app:endIconDrawable="@drawable/ic_clock"
                    app:endIconContentDescription="ToTime"
                    app:endIconTint="@color/colorAccent"
                    style="@style/OutlinedEditTextStyle"
                    android:hint="To Time">

                    <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/to_time_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:focusable="false"
                        android:cursorVisible="false" />

</com.google.android.material.textfield.TextInputLayout>

Upvotes: 9

Rohit Singh
Rohit Singh

Reputation: 18222

Disable = FOCUS+CLICK+CURSOR

Disabling focus, click, and cursor visibility does the trick for me.

Here is the code in XML

<EditText
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:cursorVisible="false"
    android:clickable="false"
    />

Upvotes: 6

Linh
Linh

Reputation: 61019

For disable edit EditText, I think we can use focusable OR enable but

  1. Using android:enabled=... or editText.setEnabled(...)

    It also changes the text color in EditText to gray.
    When clicked it have no effect

  2. Using android:focusable=... or editText.setFocusable(false) - editText.setFocusableInTouchMode(true)

    It doesn't change text color of EditText
    When clicked it highlights the EditText bottom line for about few millisecond

Output

enter image description here

Upvotes: 56

Suri
Suri

Reputation: 713

To disable the functionality of an EditText, just use:

EditText.setInputType(InputType.TYPE_NULL);

in case you want to enable it some way, then you can use:

EditText.setInputType(InputType.TYPE_CLASS_TEXT);

Upvotes: 8

Sujeet
Sujeet

Reputation: 576

Set this in your XML code, It works.

android:focusableInTouchMode="false"

Upvotes: -1

Vodet
Vodet

Reputation: 1519

As some answer mention it, if you disable the editText he become gray and if you set focusable false the cursor is displaying.

If you would like to do it only with xml this did the trick

       <YourFloatLabel
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <EditText
                android:id="@+id/view_ads_search_select"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:focusable="true"
                android:clickable="true"/>  
       </YourFloatLabel>

I simply add a FrameLayout appear above the editText and set it focusable and clickable so the editText can't be click.

Upvotes: 1

android_jain
android_jain

Reputation: 788

As android:editable="false" deprecated In xml

Use android:enabled="false" it's simple. Why use more code?

If you want in java class you can also use this programmatically

 editText.setEnabled(false);

Upvotes: 21

Favas Kv
Favas Kv

Reputation: 3099

Try this one, works fine for me:

public class CustomEdittext extends EditText {

Boolean mIsTextEditor=true;
public CustomEdittext(Context context, AttributeSet attrs) {
    super(context, attrs);
    // TODO Auto-generated constructor stub
}

@Override
public boolean onCheckIsTextEditor() {
    // TODO Auto-generated method stub
    return mIsTextEditor;
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    mIsTextEditor=false;
    Boolean mOnTouchEvent=super.onTouchEvent(event);
    mIsTextEditor=true;     
    return mOnTouchEvent;
} }

Note: You need to add this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); on your activity or else keyboard will popup at first time.

Upvotes: 1

VojtaStruhar
VojtaStruhar

Reputation: 661

There are multiple was how to achieve multiple levels of disabled.

  • editText.setShowSoftInputOnFocus(false); and editText.setFocusable

prevents the EditText from showing keyboard - writing in some text. But cursor is still visible and user can paste in some text.

  • editText.setCursorVisible(false)

hides the cursor. Not sure why would you want to do that tho. User can input text & paste.

  • editText.setKeyListener(null)

I find this way most convenient. There is no way how user can input text, but widget still works with OnClickListener if you want to trigger action when user touches it

  • editText.setEnabled(false);

completely disables EditText. It is literally 'read-only', user cannot input any text in it and (for example) OnClickListener doesn't work with it.

TextEdit documentation

Upvotes: 0

emkarachchi
emkarachchi

Reputation: 802

android:editable="false"

is now deprecated and use

 YourEditText.setInputType(InputType.TYPE_NULL);

Upvotes: 4

Mark Joshua Fajardo
Mark Joshua Fajardo

Reputation: 142

This will make your edittext disabled.

editText.setEnabled(false);

And by using this

editText.setInputType(InputType.TYPE_NULL);

Will just make your Edittext not show your softkeyboard, but if it is connected to a physical keyboard, it will let you type.

Upvotes: 5

Bhargav Thanki
Bhargav Thanki

Reputation: 4954

As android:editable="false" is depricated.You can use InputType TYPE_NULL on EditText

use like this :

editText.setInputType(InputType.TYPE_NULL);

Upvotes: 22

M.Usman
M.Usman

Reputation: 2119

you can use android:focusable="false" but also need to disable cursor otherwise copy/paste function would still work.

so, use

android:focusable="false"
android:cursorVisible="false"

Upvotes: 0

Sira Lam
Sira Lam

Reputation: 5367

Today I still use editable="false", but also with focusable="false".

I think the case we need to make an EditText un-editable, is because we want to keep its EditText style (with that underline, with hint, etc), but it accepts other inputs instead of text. For example a dropdown list.

In such use case, we need to have the EditText clickable (thus enabled="false" is not suitable). Setting focusable="false" do this trick, however, I can still long hold on the EditText and paste my own text onto it from clipboard. Depending on your code and handling this can even crash your app.

So I also used editable="false" and now everything is great, except the warning.

Upvotes: 0

Rank
Rank

Reputation: 910

Set below properties in class:

editText.setFocusable(false);
editText.setEnabled(false);

It will work smoothly as you required.

Upvotes: 5

Tom Howard
Tom Howard

Reputation: 4908

From @Asymptote's comment on the accepted answer, use:

myEditText.setEnabled(false);
myEditText.setInputType(InputType.TYPE_NULL);

...and Bob's your uncle.

Upvotes: 0

Annie
Annie

Reputation: 15

This works for me:

android:focusable="false"

Upvotes: 0

Rishabh876
Rishabh876

Reputation: 3190

In my case I needed my EditText to scroll text if no. of lines exceed maxLines when its disabled. This implementation worked perfectly for me.

private void setIsChatEditTextEditable(boolean value)
{
    if(value)
    {
        mEdittext.setCursorVisible(true);
        mEdittext.setSelection(chat_edittext.length());
       // use new EditText(getApplicationContext()).getKeyListener()) if required below
        mEdittext.setKeyListener(new AppCompatEditText(getApplicationContext()).getKeyListener());  
    }
    else
    {
        mEdittext.setCursorVisible(false);
        mEdittext.setKeyListener(null);
    }
}

Upvotes: 1

Oded Breiner
Oded Breiner

Reputation: 29759

Simply:

editText.setEnabled(false);

Upvotes: 9

Maksym Kalin
Maksym Kalin

Reputation: 1713

You can try the following method :

 private void disableEditText(EditText editText) {
    editText.setFocusable(false);
    editText.setEnabled(false);
    editText.setCursorVisible(false);
    editText.setKeyListener(null);
    editText.setBackgroundColor(Color.TRANSPARENT); 
 }

Enabled EditText :

Enabled EditText

Disabled EditText :

Disabled EditText

It works for me and hope it helps you.

Upvotes: 78

wangzhengyi
wangzhengyi

Reputation: 876

if you use android:editable="false", eclipse will remind you this message "android:editable is deprecated: Use inputType instead".

So, I use android:focusable="false" instead, it worked well for me.

Upvotes: 4

Sampath Kumar
Sampath Kumar

Reputation: 4463

Use this to disable user input

android:focusable="false"

android:editable="false" This method is deprecated one.

Upvotes: 72

Julian
Julian

Reputation: 20324

I believe the correct would be to set android:editable="false".

And if you wonder why my link point to the attributes of TextView, you the answer is because EditText inherits from TextView:

EditText is a thin veneer over TextView that configures itself to be editable.

Update:
As mentioned in the comments below, editable is deprecated (since API level 3). You should instead be using inputType (with the value none).

Upvotes: 286

Sean Android
Sean Android

Reputation: 1917

use EditText.setFocusable(false) to disable editing
EditText.setFocusableInTouchMode(true) to enable editing;

Upvotes: 190

cx0der
cx0der

Reputation: 432

Use TextView instead.

Upvotes: 3

Related Questions