Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make EditText read and copy only but not editable

I want to make my edit box read only but not editable.

User should able to copy from my Edit box but it should not be editable ny user.

please let me know how to do this.

like image 895
brig Avatar asked Feb 24 '14 11:02

brig


3 Answers

Create a TextView as has been indicated by the other answer, instead of an EditText. Then override the Activity's context menu in your Activity class as 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's text in the clipboard
     ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
     clipboard.setText(yourTextView.getText());
}

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

like image 26
u3l Avatar answered Nov 18 '22 05:11

u3l


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

textInput.setInputType(InputType.TYPE_NULL);
textInput.setTextIsSelectable(true);
textInput.setKeyListener(null);
like image 157
faris.halteh Avatar answered Nov 18 '22 04:11

faris.halteh


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

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

This will make your editText selectable but not editable.

like image 21
Hitesh Singh Avatar answered Nov 18 '22 04:11

Hitesh Singh