android-ktx
android-ktx copied to clipboard
TouchDelegate for increase the touch area.
A View extension for increase the touch area
fun View.setParentTouchDelegate(rect: Rect) {
if (parent !is View) {
throw IllegalStateException("Parent is not a View instance")
}
(parent as View).touchDelegate = TouchDelegate(rect, this)
}
fun View.increaseTouchArea(value: Int) {
val rect = Rect()
getHitRect(rect)
rect.top -= value
rect.left -= value
rect.right += value
rect.bottom += value
setParentTouchDelegate(rect)
}
Regards.!!
I also made my extension function which looks like this:
inline fun View.expandViewHitAreaBy(@Px left: Int = 0,
@Px top: Int = 0,
@Px right: Int = 0,
@Px bottom: Int = 0,
parentView: ViewGroup = (parent as ViewGroup)
) {
parentView.post {
val childRect = Rect()
this.getHitRect(childRect)
childRect.left -= left
childRect.top -= top
childRect.right += right
childRect.bottom += bottom
parentView.touchDelegate = TouchDelegate(childRect, this)
}
}