myView.getHitRect() returns coordinates in realation to parent. How to get them in relation to the grandparent?
android
Solution
If somebody had similar problem, then here is a solution which I found. The difference is flag bindToGrandparent which can be set in order to put delegate to grandparent of clickable view.
public static Runnable getTouchDelegateAction(final View delegate, final boolean bindToGrandparent, final int topPadding,
final int bottomPadding, final int leftPadding, final int rightPadding) {
return new Runnable() {
@Override
public void run() {
View parent = (View) delegate.getParent();
View grandparent = (View) parent.getParent();
// Construct a new Rectangle and let the Delegate set its values
Rect touchRect = new Rect();
delegate.getHitRect(touchRect);
if (bindToGrandparent) {
touchRect.top += parent.getTop();
touchRect.left += parent.getLeft();
touchRect.bottom += parent.getTop();
touchRect.right += parent.getLeft();
}
// Modify the dimensions of the Rectangle
// Padding values below zero are replaced by zeros
touchRect.top -= Math.max(0, topPadding);
touchRect.bottom += Math.max(0, bottomPadding);
touchRect.left -= Math.max(0, leftPadding);
touchRect.right += Math.max(0, rightPadding);
// Now we are going to construct the TouchDelegate
TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate);
// And set it on the parent
if (bindToGrandparent) {
grandparent.setTouchDelegate(touchDelegate);
} else {
parent.setTouchDelegate(touchDelegate);
}
}
};
}
It should be used as follow:
grandparentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, true, 30, 30, 30, 30));
It's important that procedure should be posted to grandparent of the view.
or:
parentView.post(FuncHelper.getTouchDelegateAction(ivFavorite, false, 30, 30, 30, 30));
Problem
I am using following code to create touch delegate of the view. Problem is that the view which clickable area I want to increase is inside of very narrow LinearLayout. So below code can increase clickable area of my view, but only in the scope of my narrow LinearLayout. I would like to pass to this function not parent but grandparent of myView (myView.getParent().getParent()). It's a RelativeLayout which has space for bigger clickable area. But then touchRect will be pointing in wrong place and my TouchDelegate will be position incorrectly.. Because: delegate.getHitRect(touchRect); returns position in relation to parent, not grandparent (or parent of parent). ``` public static Runnable getTouchDelegateAction(final View delegate, final View parent, final int topPadding, final int bottomPadding, final int leftPadding, final int rightPadding) { return new Runnable() { @Override public void run() { // Construct a new Rectangle and let the Delegate set its values Rect touchRect = new Rect(); delegate.getHitRect(touchRect); // Modify the dimensions of the Rectangle // Padding values below zero are replaced by zeros touchRect.top -= Math.max(0, topPadding); touchRect.bottom += Math.max(0, bottomPadding); touchRect.left -= Math.max(0, leftPadding); touchRect.right += Math.max(0, rightPadding); // Now we are going to construct the TouchDelegate TouchDelegate touchDelegate = new TouchDelegate(touchRect, delegate); // And set it on the parent parent.setTouchDelegate(touchDelegate); } }; } ``` Any suggestions?