Add setOnItemLongClickListener to listview in android long press

How to implement long item click listener on listview items in android app.

setOnItemLongClickListener() method is same as setOnItemClickListener() method but setOnItemLongClickListener() supports long item press functionality and you can do whatever you want to by selecting listview element. So in this tutorial we are setting up OnLongClickListener() on listview and displaying the selected long press item using toast message. So here is the complete step by step Add setOnItemLongClickListener to listview in android long press.

android-project-download-code-button

How to Add setOnItemLongClickListener to listview in android long press.

Code for MainActivity.java file.

 package com.onlongclicklistener_android_examples.com;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

 ListView listview;
 String[] subjects = new String[] {
 "Android",
 "PHP",
 "Blogger",
 "WordPress",
 "SEO"
 };
 ArrayAdapter<String> arrayadapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listview = (ListView)findViewById(R.id.listView1);

 arrayadapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, subjects);

 listview.setAdapter(arrayadapter);
 
 listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {

 @Override
 public boolean onItemLongClick(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 
 Toast.makeText(MainActivity.this, subjects[position], Toast.LENGTH_LONG).show();
 
 return true;
 }
 
 });
 
 }
 
}

Code for activity_main.xml layout file.

 <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"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.onlongclicklistener_android_examples.com.MainActivity" >

 <ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" >
 </ListView>

</RelativeLayout>

Screenshot:

Add setOnItemLongClickListener to listview in android long press

Click here to download Set OnLongClickListener on listview in android long press project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *