Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Detect Long Press in Android?

  • Last Updated : 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.

Where can we use this feature?

As mentioned, a long press can be used for plenty of applications, some are listed below:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
  1. Get information
  2. Download photos
  3. Edit pictures
  4. 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.

Long Press On a Button

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: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.Bundle
import android.widget.Button
import android.widget.Toast
import 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
        }
    }
}

Output: Run on Emulator

Long Press On a View

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.Bundle
import android.view.GestureDetector
import android.view.GestureDetector.SimpleOnGestureListener
import android.view.MotionEvent
import android.widget.Toast
import 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)
    }
}

Output: Run on Emulator




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!