shaolin
shaolin

Reputation: 473

Enabled copy on disabled EditText and explain weird behaviour of android editText

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

Upvotes: 3

Views: 415

Answers (2)

kresa
kresa

Reputation: 176

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.

Upvotes: 2

Paul Chernenko
Paul Chernenko

Reputation: 706

Try add to xml

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

And don't disable EditText

Upvotes: 1

Related Questions