Using a TouchDelegate

Showing 1-2 of 2 messages
Using a TouchDelegate Justin (Google Employee) 8/27/09 12:38 PM
I was recently asked about how to use a TouchDelegate. I was a bit
rusty myself on this and I couldn't find any good documentation on it.
Here's the code I wrote after a little trial and error.
touch_delegate_view is a simple RelativeLayout with the id
touch_delegate_root. I defined with a single, child of the layout, the
button delegated_button. In this example I expand the clickable area
of the button to 200 pixels above the top of my button.

public class TouchDelegateSample extends Activity {

  Button mButton;
  protected void onCreate(Bundle savedInstanceState) {
    mButton = (Button)findViewById(;
    View parent = findViewById(;

    // post a runnable to the parent view's message queue so its run
    // the view is drawn Runnable() {
      public void run() {
        Rect delegateArea = new Rect();
        Button delegate = TouchDelegateSample.this.mButton;
        delegate.getHitRect(delegateArea); -= 200;
        TouchDelegate expandedArea = new TouchDelegate(delegateArea,
        // give the delegate to an ancestor of the view we're
delegating the
        // area to
        if (View.class.isInstance(delegate.getParent())) {

Android Team @ Google
Re: Using a TouchDelegate Justin Kalis 1/11/17 7:52 AM
Hi Justin,

I have a question on this topic because if I were building a custom keyboard like the example on git for soft keyboard how could I employ the use of this to improve the accuracy of the hit/clicks on buttons using touch delegates when there are so many buttons with very little margin between them in Relative or Linear layouts?

There is some small issue I see when playing with the soft keyboard AOSP sample project out of git where the OEM keyboard does not exhibit, for example if I type rapid fast on the OEM Android Nougat or Below Keyboard it keeps up with no lag and the selectors that fire on click don't allow for more than one selector to highlight at once meaning if I try to press a key and hold it the key remains highlighted but if I press another at the same time the selector on the next key fires but kills the selector I was still holding down. When you do this same thing on the soft keyboard sample project both keys will highlight. I thought solving this would solve the lag but then I discovered that Google Messenger causes the lag more than other text or chat apps like WhatsApp or Skype but only with non-OEM soft keyboards.

So after looking into the touch delegates and getting an understanding of how they work, I think I may have found the solution to solve the two firing selectors to make it so it works more like OEM by using touch delegates as I was playing around with the Google keyboard and noticed touch delegates are being used. This makes sense to me because the faster performance would come from the runnable which would give higher thread priority to the key and using delegates forces the previous key press to cancel making way for rapid typing with no lag.

Could you tell me how Google does their keyboard in this regard as other OEM's like HTC, Samsung and the likes understand this no so well documented feature. I find no one making mention of this but in order to make the keyboard on a smartphone more pleasurable to use it must have some sort of thread priority to keep up with the fast typists like myself.

Since you work at Google could you look into this for me and help me out, I just want to make sure I'm on the right path here in this as I've been looking for months on how to solve the typing (input lag) explained in Google Messenger with third party sample keyboards based on soft keyboard git code. Also if you could I need help understanding predictive text and was hoping there was a way to implement this without having to reinvent the wheel maybe a library in the SDK or NDK or something else perhaps.

Thanks for the help but if you just don't have the time to reply to this I understand, no worries.

Justin Kalis