Reputation: 1043
How can I disable typing in an EditText
field in Android?
Upvotes: 92
Views: 157343
Reputation: 13960
In code:
editText.setEnabled(false);
Kotlin:
editText.isEnabled = false
Or, in XML:
android:enabled="false"
Upvotes: 49
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 :
Disabled EditText :
It works for me and hope it helps you.
Upvotes: 15
Reputation: 13
Set inputType
attribute to none in your layout.xml file under EditText
Upvotes: 0
Reputation: 574
The XML editable
property is deprecated since API LEVEL 15.
You should use now the inputType
property with the value none
.
But there is a bug that makes this functionality useless.
Here you can follow the issue status.
Upvotes: 1
Reputation: 4285
Enable:
private void enableEditText() {
mEditText.setFocusableInTouchMode(true);
mEditText.setFocusable(true);
mEditText.setEnabled(true);
}
Disable:
private void disableEditText() {
mEditText.setEnabled(false);
mEditText.setFocusable(false);
mEditText.setFocusableInTouchMode(false);
}
Upvotes: 6
Reputation: 14918
Edittext edittext = (EditText)findViewById(R.id.edit);
edittext.setEnabled(false);
Upvotes: 7
Reputation: 2412
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>
Below options doesn't work
In code:
editText.setEnabled(false);
Or, in XML:
android:editable="false"
Upvotes: 12
Reputation: 31
According to me...if you have already declared the edittext
in the XML file and set the property in the XML file "android:enabled=false"
and disable at runtime to it at that time in java file adding only 2 or 3 lines
Get by id
et1 = (EditText) FindViewById(R.id.edittext1);
et1.setEnabled(false);
You can do enable or disable to every control at run time by java file and design time by XML file.
Upvotes: 1
Reputation: 469
I think its a bug in android..It can be fixed by adding this patch :)
Check these links
question 1
and
question 2
Hope it will be useful.
Upvotes: 43
Reputation: 13
Write this in parent:
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
Example:
<RelativeLayout
android:id="@+id/menu_2_zawezanie_rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/menu_2_horizontalScrollView"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="horizontal"
android:background="@drawable/rame">
<EditText
android:id="@+id/menu2_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/menu2_ibtn"
android:gravity="center"
android:hint="@string/filtruj" >
</EditText>
<ImageButton
android:id="@+id/menu2_ibtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="@null"
android:src="@drawable/selector_btn" />
Upvotes: 0
Reputation: 407
edittext.setFocusable(false);
edittext.setEnabled(false);
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
//removes on screen keyboard
Upvotes: 2
Reputation: 1756
you can use EditText.setFocusable(false)
to disable editingEditText.setFocusableInTouchMode(true)
to enable editing;
Upvotes: 72
Reputation: 5985
You can write edittext.setInputType(0)
if you want to show the edittext but not input any values
Upvotes: 3
Reputation: 1695
The below code disables the EditText in android
editText.setEnabled(false);
Upvotes: 2
Reputation: 137322
Assuming editText
is you EditText
object:
editText.setEnabled(false);
Upvotes: 14