Add/Set item clicked effect on listview in android programmatically

How to implement clicked effect only selected listview item in android layout.

In this tutorial we are simply implementing click effect on listview. So here is the complete step by step tutorial for Add/Set item clicked effect on listview in android programmatically.

android-project-download-code-button

How to Add/Set item clicked effect on listview in android programmatically.

Code for MainActivity.java file.

 package com.setitemclickedeffectonlistview_android_examples.com;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

 ListView listview;
 String[] ListItems = new String[] {
 "ONE",
 "TWO",
 "THREE",
 "FOUR",
 "FIVE",
 "SIX",
 "SEVEN",
 "EIGHT",
 "NINE",
 "TEN"
 };
 ArrayAdapter<String> arrayadapter;
 Animation animation;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 listview = (ListView)findViewById(R.id.listView1);
 
 arrayadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_2, android.R.id.text1, ListItems);
 
 listview.setAdapter(arrayadapter); 
 
 
 listview.setOnItemClickListener(new OnItemClickListener()
 {

 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position,
 long id) {
 // TODO Auto-generated method stub
 
 animation = new AlphaAnimation(0.5f, 1.2f);
 
 animation.setDuration(2500);
 
 view.startAnimation(animation);
 
 }
 });
 }

 
}

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"
 tools:context="com.setitemclickedeffectonlistview_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"
 android:background="#02fce2" >
 </ListView>

</RelativeLayout>

Screenshot:

Add/Set item clicked effect on listview in android programmatically

Click here to download Add/Set item clicked effect on listview in android programmatically project with source code.

Leave a Reply

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