Set onclicklistener in listview in android programmatically

Android custom listview add onItemClickListener example.

On android applications listview supports both type of click functions onclicklistener() and onItemClickListener() but a major difference between both of them is onclicklistener() function set click event on whole list view user can press on any element but only single task it will do but after applying onItemClickListener() on list view it will set custom list array element item click events so developer can perform multiple tasks on every item click options. So here is the complete step by step tutorial for Set onclicklistener in listview in android programmatically.

android-project-download-code-button

Set onclicklistener in listview in android programmatically.

Code for MainActivity.java file.

 package com.example.listviewonclicklistener;
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;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

 ListView listItemView;
 
 // Define string array.
 String[] listItemsValue = new String[] {"Android","PHP","Web Development","Blogger","SEO","Photoshop"};
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listItemView = (ListView)findViewById(R.id.listView1);
 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, listItemsValue);
 
 listItemView.setAdapter(adapter); 
 
 // ListView setOnItemClickListener function apply here.
 
 listItemView.setOnItemClickListener(new OnItemClickListener()
 {
 @Override
 public void onItemClick(AdapterView<?> parent, View view,
 int position, long id) {
 // TODO Auto-generated method stub
 Toast.makeText(MainActivity.this, listItemsValue[position], Toast.LENGTH_SHORT).show();
 }
 });
 
 }

 
}

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.example.listviewonclicklistener.MainActivity" >

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

</RelativeLayout>

Screenshot:

Set onclicklistener in listview in android programmatically

Click Here To Download Set onclicklistener in listview in android programmatically project.

Leave a Reply

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