How to Make Edittext Not Editable Through Xml in Android

EditText non editable

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.

How to make EditText not editable through XML in Android?

Use this simple code:

textView.setKeyListener(null);

It works.

Edit : To add KeyListener later, do following

1 : set key listener to tag of textView

textView.setTag(textView.getKeyListener());
textView.setKeyListener(null);

2 : get key listener from tag and set it back to textView

textView.setKeyListener((KeyListener) textView.getTag());

Android - EditText not editable and not selectable

Sorry guys, at least for my setup (Sdk Version 25 with AppCompatActivity)

editText.setFocusable(false);

will still let me push the return button of the virtual keyboard, adding lines to the text. If you add

editText.setEnabled(false);

this behaviour stopps, however, the text is greyed out.

Therefore, I think the following would be a better solution:

editText.setInputType(InputType.TYPE_NULL);

How to set text in edittext which should not editable

My Question is how to put TextView in EditText?

Not Possible, because This is not Built in behavior of EditText

How is it possible?

You can use a TextView and EditText in a LinearLayout with android:orientation="horizontal"

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:background="?attr/colorBackgroundFloating"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

</LinearLayout>

OUTPUT

Sample Image

How to make edit text not editable but clickable in JAVA

The trick here is :-

et.setFocusable(false);
et.setClickable(true);

How to make edit text not editable but clickable in JAVA

The trick here is :-

et.setFocusable(false);
et.setClickable(true);

Make EditText ReadOnly

Please use this code..

Edittext.setEnabled(false);

Make all EditText not editable together

If you want to achieve this programmatically,

public void setupUI(View view, boolean editable) {
if (view instanceof EditText) {
((EditText)view).setFocusable(editable);
//Here you can add any other code that needed to be done while changing focus of a particular edit text
return;
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView, editable);
}
}
}

Provide this method with your parent view of your fragment or activity.

When you are clicking edit button pass the parent view and true(i.e, editable) to the method and after editing pass parent view and false(i.e, not editable) instead.

How to set editable true/false EditText in Android programmatically?

I did it in a easier way , setEditable and setFocusable false. but you should check this.

How to replicate android:editable="false" in code?



Related Topics



Leave a reply



Submit