0

I am wondering if I am setting up a TouchDelegate correctly. I want to increase the touch area of the TextViews inside my LinearLayout.

My layout looks like this.

<LinearLayout
        android:id="@+id/subcategory"
        android:layout_width="fill_parent"
        android:layout_height="35dp"
        android:background="#000000"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:padding="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="All"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="30dp"
            android:paddingRight="30dp"
            android:text="None"
            android:textColor="#FFFFFF"
            android:textSize="18sp" />
</LinearLayout>

I am calling the following from onActivityCreated() where subCategoryLayout is the LinearLayout

subCategoryLayout.post(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < subCategoryLayout.getChildCount(); i++) {
                        Rect delegateArea = new Rect();
                        TextView d = (TextView) subCategoryLayout.getChildAt(i);
                        d.getHitRect(delegateArea);
                        delegateArea.bottom += 50;
                        delegateArea.top += 200;
                        delegateArea.left += 50;
                        delegateArea.right += 50;

                        TouchDelegate expandedArea = new TouchDelegate(delegateArea, d);
                        subCategoryLayout.setTouchDelegate(expandedArea);
//                        if (View.class.isInstance(d.getParent())) {
//                            ((View) d.getParent()).setTouchDelegate(expandedArea);
//                        }
                    }
                }
            });
heero
  • 1,941
  • 5
  • 23
  • 33

1 Answers1

2

The first point is that View can only have one TouchDelegate. So after your for loop in fact the last TouchDelegate is set to subCategoryLayout. If you want to add several TouchDelegates to one View you can use TouchDelegateComposite

The second point is that you want to expand touch area to 50dp at the top and 200dp at the bottom. But your subCategoryLayout height is 35dp and only those touches that hit subCategoryLayout are passed to TouchDelegate. So if you want to expand touch area to greater values you have to add TouchDelegate to some parent layout which size is big enough. If you go this way you should keep in mind that you have to calculate delegateArea relatively to that bigger layout.

Community
  • 1
  • 1