यह उदाहरण दर्शाता है कि Kotlin में GestureDetector का उपयोग करके स्वाइप जेस्चर की दिशा कैसे पहचानी जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - activity_main.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="स्वाइप करके दिशा पहचानें"
android:textAlignment="center"
android:textColor="@android:color/holo_purple"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3 - MainActivity.kt में स्वाइप लिस्टनर लागू करें
import android.content.Context
import android.os.Bundle
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlin.math.abs
class MainActivity : AppCompatActivity() {
private var swipeListener: OnSwipeTouchListener? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
swipeListener = OnSwipeTouchListener(this, findViewById(R.id.relativeLayout))
}
inner class OnSwipeTouchListener(ctx: Context, mainView: View) : View.OnTouchListener {
private val gestureDetector: GestureDetector
private var context: Context
private lateinit var listener: OnSwipeListener
init {
gestureDetector = GestureDetector(ctx, GestureListener())
mainView.setOnTouchListener(this)
context = ctx
}
override fun onTouch(v: View, event: MotionEvent): Boolean {
return gestureDetector.onTouchEvent(event)
}
companion object {
private const val SWIPE_THRESHOLD = 100
private const val SWIPE_VELOCITY_THRESHOLD = 100
}
inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
override fun onDown(e: MotionEvent): Boolean = true
override fun onFling(
e1: MotionEvent, e2: MotionEvent,
velocityX: Float, velocityY: Float
): Boolean {
var result = false
try {
val diffY = e2.y - e1.y
val diffX = e2.x - e1.x
if (abs(diffX) > abs(diffY)) {
// क्षैतिज स्वाइप
if (abs(diffX) > SWIPE_THRESHOLD && abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) onSwipeRight() else onSwipeLeft()
result = true
}
} else {
// लंबवत स्वाइप
if (abs(diffY) > SWIPE_THRESHOLD && abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) onSwipeBottom() else onSwipeTop()
result = true
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return result
}
}
private fun onSwipeRight() {
Toast.makeText(context, "दाएं स्वाइप किया", Toast.LENGTH_SHORT).show()
listener.onSwipeRight()
}
private fun onSwipeLeft() {
Toast.makeText(context, "बाएं स्वाइप किया", Toast.LENGTH_SHORT).show()
listener.onSwipeLeft()
}
private fun onSwipeTop() {
Toast.makeText(context, "ऊपर स्वाइप किया", Toast.LENGTH_SHORT).show()
listener.onSwipeTop()
}
private fun onSwipeBottom() {
Toast.makeText(context, "नीचे स्वाइप किया", Toast.LENGTH_SHORT).show()
listener.onSwipeBottom()
}
interface OnSwipeListener {
fun onSwipeRight()
fun onSwipeLeft()
fun onSwipeTop()
fun onSwipeBottom()
}
}
}
चरण 4 - AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.swipedetection">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस पर ऐप चलाएं। स्क्रीन पर किसी भी दिशा में स्वाइप करें - टोस्ट संदेश स्वाइप की दिशा दिखाएगा (बाएं, दाएं, ऊपर, नीचे)।