Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a Button more responsive?

I have noticed that some Buttons don't seem as responsive as they could be. This applies equally to my app and to most other apps I've tried.

When I press a Button there is a tiny bit of lag (EDIT: I would estimate about 20-50 ms) before the Button lights up in the pressed state. Some apps have managed to remove this bit of lag, for instance RealCalc (available in the Market) whose Buttons switch to the pressed state immediately after you press your finger on them.

Most of the time this lag is not noticeable, but in my case where the Buttons are used in a custom number pad, this tiny bit of lag is disruptive to the user. RealCalc feels so much more responsive and polished because this lag has been removed.

My question is - how do I remove this lag? I am aware that I could just subclass, override onTouchEvent and proceed from there, but I would really prefer a solution using just the standard controls and options. I suspect the solution may also interfere with scrolling, but I can live with that.

EDIT: Specifically, the lag mentioned is the time from you put your finger on a button and hold it there until the button switches to the pressed state. The onClick handler is called when you remove your finger again.

Some answers suggested moving the bulk of my onClick handler to a thread. This is not the issue. To make doubly sure, I have removed all click handlers, and the tiny lag is still there.

like image 815
Carsten Avatar asked Feb 18 '11 10:02

Carsten


1 Answers

I have dug into the Android source code to see what was going on.

It turns out that the android.view.View class (from which Button derives) enters a "PREPRESSED" state before going into the PRESSED state:

android.view.View:

1529    /**
1530      * Indicates a prepressed state;
1531      * the short time between ACTION_DOWN and recognizing
1532      * a 'real' press. Prepressed is used to recognize quick taps
1533      * even when they are shorter than ViewConfiguration.getTapTimeout().
1534      * 
1535      * @hide
1536      */
1537     private static final int PREPRESSED             = 0x02000000;

android.view.ViewConfiguration.getTapTimeout() is 115 ms on my Nexus One, which is a lot longer than my estimation.

android.view.ViewConfiguration:

67     /**
68      * Defines the duration in milliseconds we will wait to see if a touch event 
69      * is a tap or a scroll. If the user does not move within this interval, it is
70      * considered to be a tap. 
71      */
72     private static final int TAP_TIMEOUT = 115;

Anyway, from examining View.onTouchEvent is doesn't look like there's a way to avoid this PREPRESSED state by any standard option. Which is a real shame.

The good news is that I have now verified that the way to avoid this lag is to subclass and override onTouchEvent.

Thanks for the discussion and answers.

like image 154
Carsten Avatar answered Oct 06 '22 01:10

Carsten