android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

TouchDelegate for increase the touch area.

Open tebitoq opened this issue 8 years ago • 1 comments

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.!!

tebitoq avatar Feb 07 '18 21:02 tebitoq

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)
    }
}

DoruAdryan avatar May 11 '18 13:05 DoruAdryan