Reputation: 2713
I'm trying to make an EditText non editable with this code:
<EditText android:id="@+id/outputResult"
android:inputType="text"
android:editable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/result" />
I had to add following line to make it non-editable
android:focusable="false"
Am I doing something wrong?
Upvotes: 88
Views: 214618
Reputation: 199
well android:editable="false"
is deprecated and doesn't work anymore and android:inputType="none"
would not give you the results you want
you could use
<EditText
android:cursorVisible="false"
android:focusableInTouchMode="false"
android:maxLength="10"/>
and if you want to make the EditText editable again you could do that with code
//enable view's focus event on touch mode
edittext.setFocusableInTouchMode(true);
//enable cursor is visible
edittext.setCursorVisible(true);
// You have to focus on the edit text to avoid having to click it twice
//request focus
edittext.requestFocus();
//show keypad is used to allow the user input text on the first click
InputMethodManager openKeyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
openKeyboard.showSoftInput(edittext, InputMethodManager.SHOW_IMPLICIT);
Note I'm using android:focusableInTouchMode="false"
and not
android:focusable="false"
because when I use
edittext.setFocusable(true);
I still have to add
edittext.setFocusableInTouchMode(true);
Like this
edittext.setFocusableInTouchMode(true);
edittext.setFocusable(true);
for the edittext focus to be enabled again
Upvotes: 10
Reputation: 132
EditText edtText = (EditText) findViewById(R.id.yourEditTextId);
edtText.setEnabled(false);
and you can use android:enabled
property in your xml file as well.
Upvotes: 3
Reputation: 10549
You may need to achieve an edit text behavior in Andorid TV were you need to make a non editable edit text but clickable, the below method helps you to achieve that -
/**
* Disable edit text input skill
*/
fun EditText.disableInput() {
showSoftInputOnFocus = false
keyListener = null // Make non editable
// Hide keyboard when disabled edittext gets focus
setOnFocusChangeListener { _, hasFocus ->
if (hasFocus) hideKeyboard()
}
}
fun View.hideKeyboard() {
val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}
You can get the click event using the conventional onClick
listener thereafter
Upvotes: 1
Reputation: 441
If you want your edit text clickable but not editable, You can try this:
android:cursorVisible="false"
android:inputType="none"
android:clickable="true"
android:focusable="false"
Upvotes: 2
Reputation: 47
If not necessary, try removing the inputType attribute. android:editable="false"
works fine.
Upvotes: 0
Reputation: 376
android:inputType="none"
android:enabled="false"
android:clickable="false"
The clickable attribute is optional! You can still set text to the edit text programmatically!
Upvotes: 1
Reputation: 419
Set this attribute in your edittext.
<EditText
android:editable="false"
/>
Upvotes: 0
Reputation: 3070
I was unable to get rid of SoftKeyboard for the used EditText
in the Tablet version and the following code snippet did the job very well.
eText.setEnabled(false);
eText.setFocusable(false);
eText.setActivated(false);
eText.setInputType(InputType.TYPE_NULL);
Upvotes: 0
Reputation: 4191
If you want your disabled EditText to look the same as your enabled one, you should use android:enabled="false"
in combination with android:textColor="..."
.
Here is an example:
<com.google.android.material.textfield.TextInputLayout
...>
<com.google.android.material.textfield.TextInputEditText
...
android:text="..."
android:enabled="false"
android:textColor="@color/dark_grey"/>
</com.google.android.material.textfield.TextInputLayout>
Upvotes: 2
Reputation: 571
android:enabled = false
Include that line in your layout xml file.
Upvotes: 2
Reputation: 1693
My requirement was to have an edittext look and feel and to open a pop up window on its click, but making enabled as false will not allow click listener on it. So I did it in this way.
<EditText
android:id="@+id/tvCustomerAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="80dp"
android:layout_alignParentEnd="true"
android:drawableRight="@drawable/arrow_down"
android:inputType="none"
android:focusable="false"
android:clickable="true"
tools:text="24"/>
Upvotes: 0
Reputation: 1073
If you really want to use an editText and not a textView, then consider using these three:
android:focusable="false"
android:clickable="false"
android:longClickable="false"
EDIT: The "longClickable" attribute disables the long press actions that cause the "Paste" and or "Select All" options to show up as a tooltip.
Upvotes: 7
Reputation: 11
I had this problem, too.
I did this:
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:id="@+id/yourid"
android:editable="false"
android:inputType="none" />
Upvotes: 1
Reputation: 61
Add below line in youe xml edittext.
android:editable="false"
android:focusable="false"
android:clickable="false"
Its working for me
Upvotes: 3
Reputation: 3021
I guess I am late but use following together to make it work:
android:inputType="none"
android:enabled="false"
Upvotes: 36
Reputation: 36007
In case you want to make an EditText not editable from code:
void disableInput(EditText editText){
editText.setInputType(InputType.TYPE_NULL);
editText.setTextIsSelectable(false);
editText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v,int keyCode,KeyEvent event) {
return true; // Blocks input from hardware keyboards.
}
});
}
In case you want to remove the horizontal line of your EditText, just add:
editText.setBackground(null);
If you want to let users copy the content of the EditText then use:
editText.setTextIsSelectable(true);
Upvotes: 18
Reputation: 2457
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editTexteventdate"
android:background="@null"
android:hint="Date of Event"
android:textSize="18dp"
android:textColor="#fff"
android:editable="false"
android:focusable="false"
android:clickable="false"
/>
Add this
android:editable="false"
android:focusable="false"
android:clickable="false"
its work for me
Upvotes: 11
Reputation: 4812
android:editable="false"
should work, but it is deprecated, you should be using android:inputType="none"
instead.
Alternatively, if you want to do it in the code you could do this :
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setEnabled(false);
This is also a viable alternative :
EditText mEdit = (EditText) findViewById(R.id.yourid);
mEdit.setKeyListener(null);
If you're going to make your EditText
non-editable, may I suggest using the TextView
widget instead of the EditText
, since using a EditText seems kind of pointless in that case.
EDIT: Altered some information since I've found that android:editable
is deprecated, and you should use android:inputType="none"
, but there is a bug about it on android code; So please check this.
Upvotes: 206