Using a TouchDelegate

Showing 1-1 of 1 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;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.touch_delegate_view);
    mButton = (Button)findViewById(R.id.delegated_button);
    View parent = findViewById(R.id.touch_delegate_root);

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

Cheers,
Justin
Android Team @ Google