How to Detect Long Press in Android?

23 Feb, 2021
A Long Press refers to pressing a physical button or tap a virtual button on a touchscreen and holding it down for a second or two. Employed on touchscreens, smartphones, tablets, and smartwatches, the long press or long tap increases the user interface’s flexibility. The typical “short press” or “short tap” performs one operation, while pressing/tapping and holding that same button for a short time activates another. Long pressing lets you get some information, download photos from the web, edit pictures, and more.
As mentioned, a long press can be used for plenty of applications, some are listed below:
Get informationDownload photosEdit picturesCopy, Cut, Paste operations on Text View
Get information
Download photos
Edit pictures
Copy, Cut, Paste operations on Text View
Through this article, we want to extend our knowledge regarding the long press on a Button as well as a view such as a TextView in Android. We have implemented a method that would detect a long press for a certain duration, and if the criteria are fulfilled, a Toast would be generated. Note that we are going to implement this project using the Kotlin language.
To detect a long press on a button in Android, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file which represents the UI of the application, and create a Button that on long-press would generate a Toast. Below is the code for the activity_main.xml file. 
XML
<?xml version="1.0" encoding="utf-8"?><RelativeLayout     xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity">      <!--This button has to be long-clicked-->    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="hold" />  </RelativeLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundleimport android.widget.Buttonimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          // Declare a button        val mBtn = findViewById<Button>(R.id.btn)          // implement a setOnLongClickListener to the           // button that creates a Toast and        // returns true when actually long-pressed        mBtn.setOnLongClickListener {            Toast.makeText(applicationContext, "Button Long Pressed", Toast.LENGTH_SHORT).show()            true        }    }}
To detect a long press on a screen in Android, follow the following steps:
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. There is nothing to do with the activity_main.xml file. So keep the file as it is. (Assuming that every new project creates a textView in the layout by default)
Step 2: Working with the MainActivity.kt file
Go to the MainActivity.kt file, and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.os.Bundleimport android.view.GestureDetectorimport android.view.GestureDetector.SimpleOnGestureListenerimport android.view.MotionEventimport android.widget.Toastimport androidx.appcompat.app.AppCompatActivity  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)        // Do Nothing    }          // GestureDetecctor to detect long press    private val gestureDetector = GestureDetector(object : SimpleOnGestureListener() {        override fun onLongPress(e: MotionEvent) {              // Toast to notify the Long Press            Toast.makeText(applicationContext, "Long Press Detected", Toast.LENGTH_SHORT).show()        }    })      // onTouchEvent to confirm presence of Touch due to Long Press    override fun onTouchEvent(event: MotionEvent?): Boolean {        return gestureDetector.onTouchEvent(event)    }}
Android-Misc
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android SDK and it's Components
How to Communicate Between Fragments in Android?
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
How to Communicate Between Fragments in Android?
Kotlin Array
Retrofit with Kotlin Coroutine in Android