3

My question is: Is it possible to enable copy text function from a disabled editText ?

I've tried the following code to test the behaviour on Android 4.4.2 (samsung Galaxy Note II)

EditText _edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment_test);

    _edit = (EditText) findViewById(R.id.editDisabled);
    _edit.setText("Text to be copied...");
    _edit.setEnabled(false);

    /* update code with answer below */
    _edit.setInputType(InputType.TYPE_NULL);
    _edit.setTextIsSelectable(true);
    /* end mod */


    Toast.makeText(getApplicationContext(), "onClick enabled: " + (_edit.isClickable() ? true : false) + 
                                        " \n onLongClick enabled: " +  (_edit.isLongClickable() ? true : false) , Toast.LENGTH_LONG).show();


    _edit.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View paramView) {
            Toast.makeText(getApplicationContext(), "onLongClick()!!!", Toast.LENGTH_LONG).show();
            return false;
        }
    });

    _edit.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View paramView) {
            Toast.makeText(getApplicationContext(), "onclick()!!", Toast.LENGTH_LONG).show();

        }
    });
}

When the activity is open, the Toast displays true/true for the onClick() and onLongClick() events. But if I try to click or longClick on the disabled editText no one event is fired..

So is anyone able to answer to my question or explain the strange behaviour of the disabled editText?

Thank you in advance

shaolin
  • 463
  • 5
  • 13
  • ...it's not a duplicate...I'm trying to ENABLE the copy function from the editText not to disable as pointed out in that thread...please read the question carefully before answering... – shaolin Mar 18 '15 at 13:42

2 Answers2

2

It is not strange behaviour. Disabling view means forbidding user interaction, no matter if view is (long) clickable.

You can get do as Paul Chernenko answered, to disable input and leave text selectable. User interaction will also be possible(click and long click).

In your case, I assume the only thing you are missing from disabled state of EditText is appearance as disabled. That can be achieved by customizing EditText Style which would be another question.

Community
  • 1
  • 1
kresa
  • 176
  • 2
  • 11
  • thanks Mllos. The strange behaviour comes from the test isClickable() and then I cannot fire the onClick event...but maybe that's my personal opinion As you pointed out , the best I can do is to set a custom style for the editText, I don't have any other solutions I suppose.. so how to style such an editText to appear as a disabled one, mantaining the copy function? – shaolin Mar 18 '15 at 14:41
  • 1
    you can explain that "strange" behaviour like "view is clickable but if it is enabled". enable attribute is one level above clickable and it has priority. Glad to help. – kresa Mar 18 '15 at 14:43
1

Try add to xml

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

And don't disable EditText

Paul Chernenko
  • 696
  • 5
  • 21
  • tried, but nothing change. I'm gonna update the question with this code. Thanks ! ...I need to disable editText mate...this is my problem... – shaolin Mar 18 '15 at 14:00
  • @vince46 but it also won't be possible to type something there, and you can create some listener to prevent pasting anything to your EditText – Paul Chernenko Mar 18 '15 at 14:24
  • -I need the editText to be disabled, because I've already put some content before and now after some other events I show it with code - I need to show the editText as disabled for style requirements and not let ting the user change the content - The content of the editText needs to be copied by the user for other purposes – shaolin Mar 18 '15 at 14:37