Android EditText inputType="none" doesn't work, becomes "textMultiLine"
Asked Answered
M

8

20

Tested with Android 1.6(4) and 2.3.3(10).

I've made a minimalistic test application to demonstrate this, all it does is load the xml with:

setContentView(R.layout.main); 

and the xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:ems="10" >

</EditText>

The problem:

when setting inputType="none" the actual input type during execution becomes textMultiLine(0x00020001), I've checked it with a debugger.

On the other hand if I use inputType="text" it works as expected.

Is this a bug in Android?

Matri answered 18/4, 2012 at 0:7 Comment(0)
C
37

I had the same problem: defining the input type via xml did not work.

Then, to fix it, I set the input type programatically:

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState)
    {
    ...
    editText1= (EditText) super.getActivity().findViewById(R.id.editText1);
    editText1.setInputType(InputType.TYPE_NULL);    // none...
    ...
    }

That works for me.

Colorado answered 10/8, 2015 at 12:35 Comment(2)
better to use setInputType(InputType.TYPE_NULL)Breastwork
this works, but in order to get the click listener to trigger i have to click twice. ideas? i already tried setting focusable and focusableInTouchMode to falseCathouse
H
14

Setting InputType="none" xml doesn't work, but if you're using databinding then you can use the binding syntax to set Input type.

import the type..

<data> <import type="android.text.InputType" /> </data>

then bind the value

<EditText android:id="@+id/edit_text" android:inputType="@{InputType.TYPE_NULL}" />

Hyrax answered 6/7, 2017 at 13:3 Comment(2)
With Material Library's TextInputEditText, setting inputType to none or editable to false doesn't help, but this works fine. The first answer is to do this in Java/Kotlin.Bradfield
this works, but in order to get the click listener to trigger i have to click twice. ideas? i already tried setting focusable and focusableInTouchMode to falseCathouse
C
13

Use android:editable="false". Even though it's deprecated, it works when inputType doesn't.

Cushiony answered 25/2, 2014 at 14:55 Comment(3)
Yes you are true. This is just the right works. Because inputType="none" is nonsense of deprecation update.Commit
Actually this is the right answer, because EditText is editable = true by default and inputType is changed to TEXT in that case, it doesn't care of "none" in that case as this is also default value. See code of TextView ``` else if (inputType != EditorInfo.TYPE_NULL) { ... } ... else if (editable) { createEditorIfNeeded(); mEditor.mKeyListener = TextKeyListener.getInstance(); mEditor.mInputType = EditorInfo.TYPE_CLASS_TEXT; } ```Roebuck
@Commit Yes you are right. inputType="none" never works for anyone. You have to set it programatically in code using InputType.TYPE_NULL or resort to the deprecated android:editable="false" property in xmlNebuchadnezzar
T
11

From what I can tell this is a bug in the android code. see this for refrence - How to make EditText not editable through XML in Android?

GalDude33 suggests-

    android:clickable="false" 
    android:cursorVisible="false" 
    android:focusable="false" 
    android:focusableInTouchMode="false"
Terryterrye answered 18/4, 2012 at 2:12 Comment(0)
M
4

Use this

 textView.setKeyListener(null);
Mclyman answered 10/3, 2014 at 7:29 Comment(2)
You sir, are my hero. I tried all the other suggested solutions above and some others but this is a thing of beauty. All the other methods had a trade-off except this.Imitative
how to enable it back?Ader
P
3

Setting the .xml like this works, however, I had to add another line because the cursor was visible.

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

It's strange that even after 2 years this "bug" is present, and the depreciated method works better than the suggested one.

Using this works as well, but you have to edit the background color in that case, unless you want the color different.

android:enabled="false"
Photoreceptor answered 12/7, 2021 at 10:53 Comment(0)
G
0

The following code works:

Use android:enabled="false"

Gowk answered 4/6, 2021 at 14:33 Comment(0)
P
0

This is a working example:-

<AutoCompleteTextView
                    android:id="@+id/autoComplete"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:inputType="none"
                    android:enabled="false"
                    android:padding="15dp" />
Prelatism answered 5/8, 2022 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.