How to make EditText read-only and not editable

I want the edit window to be read-only, but not editable.

The user should copy from my edit window, but it should not be editable.

please let me know how to do this.

+6
source share
6 answers

You can overwrite the key listener so you can do anything other than editing

final EditText edittext = (EditText) findViewById(R.id.edittext); edittext.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { return true; } }); 
0
source

The text.setTextIsSelectable(true) command requires API 11. For those using a lower API, use the following XML:

 android:inputType="none" android:textIsSelectable="true" 

This will make your editText accessible but not editable.

+9
source

The easiest way to do this is to add this code:

 textInput.setInputType(InputType.TYPE_NULL); textInput.setTextIsSelectable(true); textInput.setKeyListener(null); 
+2
source

Create a TextView as indicated by another answer, instead of EditText . Then redefine the Activity context menu in your Activity class, as shown below:

 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { menu.add(0, v.getId(), 0, "Copy"); //cast the received View to TextView so that you can get its text TextView yourTextView = (TextView) v; //place your TextView text in the clipboard ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); clipboard.setText(yourTextView.getText()); } 

Then just call registerForContextMenu(yourTextView); in onCreate() .

+1
source

Use the android: editable = "false" property for EditText in the layout view file.

0
source

Me using this class

 import android.content.Context; import android.text.InputFilter; import android.text.Spanned; import android.util.AttributeSet; import android.widget.EditText; /* * * To make EditText read and copy only but not editable * using * sendReadOnlyCallback(callback); * */ public class MyEditText extends EditText { private InputFilter[] originalFilters = null; private boolean internalChange = false; private InputFilter[] myInputFilters = null; private static ReadonlyCallback sDummyCallback = new ReadonlyCallback() { @Override public boolean isReadOnly() { return false; } }; private ReadonlyCallback callback = sDummyCallback; public MyEditText(Context context) { super(context); } public MyEditText(Context context, AttributeSet attrs) { super(context, attrs); } public MyEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public interface ReadonlyCallback { public boolean isReadOnly(); } public void setReadonlyCallback(ReadonlyCallback cb) { if (cb == null) callback = sDummyCallback; else callback = cb; } public void setFilters(InputFilter[] filters) { // duplicated from TexView originalFilters = new InputFilter[filters.length]; System.arraycopy(filters, 0, originalFilters, 0, filters.length); // funny No. 1 : have to re instantiate `callback` // otherwise got `NullPointerExcection` when called from `filter` callback = sDummyCallback; myInputFilters = new InputFilter[] { new InputFilter() { // funny No. 2: // have to make refs to `originalfilters` // otherwise got `NullPointerExcection` when called from `filter` InputFilter[] flts = originalFilters; @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (!internalChange && callback.isReadOnly()) return dest.subSequence(dstart, dend); int filtercount = flts.length; if (filtercount == 0) return null; // duplicated from InputFilter.AllCaps for (int i = 0; i < filtercount; i++) { CharSequence repl = flts[i].filter(source, start, end, dest, start, end); if (repl != null) { source = repl; start = 0; end = repl.length(); } if (i == filtercount) return repl; } return null; } } }; super.setFilters(myInputFilters); } @Override public InputFilter[] getFilters() { if (myInputFilters == null) return super.getFilters(); return originalFilters; } @Override public synchronized void setText(CharSequence text, BufferType type) { internalChange = true; super.setText(text, type); internalChange = false; } } 
0
source

All Articles