53

I have a EditText which I am presenting as disabled so that user cannot edit the contents and also to avoid popping-up keyboard. But now I want to capture onclick event in the EditText and identify the individual words that are being clicked. Any help is most welcome.

Thanks, Balkrishna.

Balkrishna Rawool
  • 1,865
  • 3
  • 21
  • 35

2 Answers2

120

On your EditText set attributes android:focusable="false" and android:enabled="true". And set OnClickListener to that EditText. The click on that Edittext triggers the onClick() method but it can't be edited. I just tried it.

Rajkiran
  • 15,845
  • 24
  • 74
  • 114
  • thanks Rajkiran.. now I can get the onclick event.. but the next problem is to get hold on to the word being clicked.. – Balkrishna Rawool Mar 15 '12 at 17:44
  • Well, I don't see any way to achieve that. You will have to play around with it. And don't forget to post the answer if you get the solution for the same. Best luck! – Rajkiran Mar 16 '12 at 14:14
  • 8
    Also set android:longClickable="false", without on long click it would go into select/paste mode – Ihor Klimov Jan 03 '18 at 12:46
  • 2
    You'll also want to consider `android:cursorVisible="false"` to avoid the user from seeing a real cursor for a quick second when clicking the EditText. – Anthony Chuinard Sep 28 '20 at 04:34
6

You can make the component unclickable by using.

editText.setClickable(false);

To make the component look visually disabled by decreasing the Alpha value.

editText.setAlpha(0.5f);

It will allow you to set an OnTouchListener on your component by which you can perform your desired operations.

editText.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Toast.makeText(getApplicationContext(),"Component is disabled", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                });
Omer Shafiq
  • 223
  • 2
  • 6