Kotlin OnTouchListener called but it does not override performClick

lambda picture lambda · Nov 8, 2017 · Viewed 52.7k times · Source

How to override performClick in Kotlin to avoid warning?

next.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
                val icon: Drawable = ContextCompat.getDrawable(activity.applicationContext, R.drawable.layer_bt_next)
                icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY)
                next.setImageDrawable(icon)
            }
            MotionEvent.ACTION_UP -> {
                //view.performClick()
                next.setImageResource(R.drawable.layer_bt_next)
            }
        }
        return@OnTouchListener true
    })

view.performClick does not work.

Answer

Manohar Reddy picture Manohar Reddy · May 22, 2018

Try this way :

 next.setOnTouchListener(object : View.OnTouchListener {
        override fun onTouch(v: View?, event: MotionEvent?): Boolean {
            when (event?.action) {
                MotionEvent.ACTION_DOWN -> //Do Something
            }

            return v?.onTouchEvent(event) ?: true
        }
    })