Android Open Source - custom-touch-examples Touch Delegate Layout

From Project

Back to project page custom-touch-examples.


The source code is released under:

Copyright (c) 2012 Wireless Designs, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in ...

If you think the Android project custom-touch-examples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

 * Copyright (c) 2012 Wireless Designs, LLC
 */*from w  w w . j a va 2  s  .c  om*/
 * See the file license.txt for copying permission.
package com.examples.customtouch.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.TouchDelegate;
import android.widget.CheckBox;
import android.widget.FrameLayout;

public class TouchDelegateLayout extends FrameLayout {

  public TouchDelegateLayout(Context context) {

  public TouchDelegateLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

  public TouchDelegateLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  private CheckBox mButton;
  private void init() {
    mButton = new CheckBox(getContext());
    mButton.setText("Click Anywhere On Screen");
    addView(mButton, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER));

   * TouchDelegate is applied to this view (parent) to delegate all touches
   * within the specified rectangle to the CheckBox (child).  Here, the rectangle
   * is the entire size of this parent view.
   * This must be done after the view has measured itself so we know how big to make the rect,
   * thus we've chosen to add the delegate in onMeasure()
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //Apply the whole area of this view as the delegate area
    Rect bounds = new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight());
    TouchDelegate delegate = new TouchDelegate(bounds, mButton);

Java Source Code List