{"id":193,"date":"2021-06-22T05:53:45","date_gmt":"2021-06-22T05:53:45","guid":{"rendered":"https:\/\/barahasoft.com.np\/blog\/?post_type=codings&#038;p=193"},"modified":"2021-06-22T05:53:47","modified_gmt":"2021-06-22T05:53:47","slug":"how-to-detect-swipe-in-kotlin-android","status":"publish","type":"codings","link":"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/","title":{"rendered":"How to detect swipe in kotlin (Android)"},"content":{"rendered":"\n<p>In this post we gonna see the easiest way to handle event like left \/right\/top\/bottom swipe for any view .<\/p>\n\n\n\n<p>This class (OnSwipeTouchListener.kt) can be used for detection.<\/p>\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage np.com.barahasoft.ourbarahakshetra.common\n\nimport android.content.Context\nimport android.view.GestureDetector\nimport android.view.MotionEvent\nimport android.view.View\nimport java.lang.Math.abs\n\ninternal open class OnSwipeTouchListener(c: Context?) :\n        View.OnTouchListener {\n    private val gestureDetector: GestureDetector\n    override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {\n        return gestureDetector.onTouchEvent(motionEvent)\n    }\n    private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {\n        private val SWIPE_THRESHOLD: Int = 100\n        private val SWIPE_VELOCITY_THRESHOLD: Int = 100\n        override fun onDown(e: MotionEvent): Boolean {\n            return true\n        }\n        override fun onSingleTapUp(e: MotionEvent): Boolean {\n            onClick()\n            return super.onSingleTapUp(e)\n        }\n        override fun onDoubleTap(e: MotionEvent): Boolean {\n            onDoubleClick()\n            return super.onDoubleTap(e)\n        }\n        override fun onLongPress(e: MotionEvent) {\n            onLongClick()\n            super.onLongPress(e)\n        }\n        override fun onFling(\n                e1: MotionEvent,\n                e2: MotionEvent,\n                velocityX: Float,\n                velocityY: Float\n        ): Boolean {\n            try {\n                val diffY = e2.y - e1.y\n                val diffX = e2.x - e1.x\n                if (abs(diffX) &gt; abs(diffY)) {\n                    if (abs(diffX) &gt; SWIPE_THRESHOLD &amp;amp;&amp;amp; abs(\n                                    velocityX\n                            ) &gt; SWIPE_VELOCITY_THRESHOLD\n                    ) {\n                        if (diffX &gt; 0) {\n                            onSwipeRight()\n                        }\n                        else {\n                            onSwipeLeft()\n                        }\n                    }\n                }\n                else {\n                    if (abs(diffY) &gt; SWIPE_THRESHOLD &amp;amp;&amp;amp; abs(\n                                    velocityY\n                            ) &gt; SWIPE_VELOCITY_THRESHOLD\n                    ) {\n                        if (diffY &lt; 0) {\n                            onSwipeUp()\n                        }\n                        else {\n                            onSwipeDown()\n                        }\n                    }\n                }\n            } catch (exception: Exception) {\n                exception.printStackTrace()\n            }\n            return false\n        }\n    }\n    open fun onSwipeRight() {}\n    open fun onSwipeLeft() {}\n    open fun onSwipeUp() {}\n    open fun onSwipeDown() {}\n    private fun onClick() {}\n    private fun onDoubleClick() {}\n    private fun onLongClick() {}\n    init {\n        gestureDetector = GestureDetector(c, GestureListener())\n    }\n}\n<\/pre>\n\n\n<p>Now let&#8217;s see how we use above class <\/p>\n\n\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nbinding!!.wvDetails.setOnTouchListener(object : OnSwipeTouchListener(this@NewsDetailActivity) {\n            override fun onSwipeLeft() {\n                super.onSwipeLeft()\n                Log.d(TAG, &quot;onSwipeLeft: &quot;)\n                \n            }\n            override fun onSwipeRight() {\n                super.onSwipeRight()\n                Log.d(TAG, &quot;onSwipeRight: &quot;)\n            }\n            override fun onSwipeUp() {\n                super.onSwipeUp()\n                Log.d(TAG, &quot;onSwipeUp: &quot;)\n            }\n            override fun onSwipeDown() {\n                super.onSwipeDown()\n                Log.d(TAG, &quot;onSwipeDown: &quot;)\n            }\n        })\n<\/pre>","protected":false},"featured_media":0,"parent":0,"template":"","tags":[],"language":[36],"ctype":[40],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to detect swipe in kotlin (Android) - BS Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to detect swipe in kotlin (Android) - BS Blog\" \/>\n<meta property=\"og:description\" content=\"In this post we gonna see the easiest way to handle event like left \/right\/top\/bottom swipe for any view . This class (OnSwipeTouchListener.kt) can be used for detection. Now let&#8217;s see how we use above class\" \/>\n<meta property=\"og:url\" content=\"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/\" \/>\n<meta property=\"og:site_name\" content=\"BS Blog\" \/>\n<meta property=\"article:modified_time\" content=\"2021-06-22T05:53:47+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/\",\"name\":\"BS Blog\",\"description\":\"Baraha Soft Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/barahasoft.com.np\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/#webpage\",\"url\":\"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/\",\"name\":\"How to detect swipe in kotlin (Android) - BS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/barahasoft.com.np\/blog\/#website\"},\"datePublished\":\"2021-06-22T05:53:45+00:00\",\"dateModified\":\"2021-06-22T05:53:47+00:00\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/barahasoft.com.np\/blog\/codings\/how-to-detect-swipe-in-kotlin-android\/\"]}]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings\/193"}],"collection":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/codings"}],"about":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/types\/codings"}],"wp:attachment":[{"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/media?parent=193"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/tags?post=193"},{"taxonomy":"language","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/language?post=193"},{"taxonomy":"ctype","embeddable":true,"href":"https:\/\/barahasoft.com.np\/blog\/wp-json\/wp\/v2\/ctype?post=193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}