The objective of this article is to create an Android application which handles the long click event on the listfragment items. If you want to see the screenshot of this application, please jump to the end of this article
1. Create an Android Project namely “ListFragmentItemLongClick”
2. Select Android build target for this application
3. Enter application details
4. src/in/wptrafficanalyzer/listfragmentitemlongclick/CountryListFragment.java
package in.wptrafficanalyzer.listfragmentitemlongclick; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ArrayAdapter; import android.widget.Toast; public class CountryListFragment extends ListFragment{ String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { ArrayAdapter<String> adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,countries); setListAdapter(adapter); return super.onCreateView(inflater, container, savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); OnItemLongClickListener listener = new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) { Toast.makeText( getActivity().getBaseContext() , "Long Clicked " + countries[position] , Toast.LENGTH_SHORT).show(); return false; } }; getListView().setOnItemLongClickListener(listener); } }
5. res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <fragment android:id="@+id/country_list_fragment" android:name="in.wptrafficanalyzer.listfragmentitemlongclick.CountryListFragment" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
6. Application in execution
7. Downloads

8. Reference
http://developer.android.com/guide/index.html

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in
You can hire me on hourly basis or on project basis for Android applications development.
For hiring me, please mail your requirements to info@wptrafficanalyzer.in.
My other blogs
store4js.blogspot.com