Search code examples
javaandroidlistenertouch-eventdisable

How to disable the touch listener for some duration in Android


I have a code which has set of buttons listening for touch events.

for (int i = 0; i < mybtn.length; i++) {
    String btnid = "btn" + i;
    int resid = getResources().getIdentifier(btnid, "id", getPackageName());
    mybtn[i] = (Button) findViewById(resid);
    mybtn[i].setOnTouchListener(this);
}

But I am also using the TTS engine and I need to synchronize the speaking events with the touch events.

For that purpose I need to disable the touch listener for the buttons for some duration and then enable them after my work is done.

I want to write a method which can enable and disable touch events as per my requirements.


Solution

  • To disable the Android touch listener you need to disable below properties...

     btn.setFocusable(false);
    
     btn.setClickable(false);
    

    or

     btn.setEnabled(false);