Hello friends,
Today I am going to share very Important code for Multi-Clickable Listview in Android.
Suppose you want perform multiple task on click of Listview like- edit, delete, share.For this I am using custom Array-adapter. Screen shot and code is given below step by step-
1. List View Clicked
2. Edit Button Clicked
3. Delete Button Clicked
activity_main.xml:
<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:background="@android:color/white"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="Multi Touch Listview"
android:textColor="#0099CC"
android:textSize="20sp" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/textView"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000" />
</RelativeLayout>
row.xml
<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:padding="5dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="User Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="5dp"
android:text="Address"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit"
android:textColor="@android:color/black" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="3dp"
android:background="@android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Delete"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="5dp"
android:text="Location" />
</RelativeLayout>
Note- Don't forget to add below line in Button View-
android:focusable="false"
android:focusableInTouchMode="false"
I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)
Today I am going to share very Important code for Multi-Clickable Listview in Android.
Suppose you want perform multiple task on click of Listview like- edit, delete, share.For this I am using custom Array-adapter. Screen shot and code is given below step by step-
1. List View Clicked
2. Edit Button Clicked
3. Delete Button Clicked
activity_main.xml:
<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:background="@android:color/white"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"
android:text="Multi Touch Listview"
android:textColor="#0099CC"
android:textSize="20sp" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/textView"
android:layout_marginTop="5dp"
android:background="@android:color/transparent"
android:cacheColorHint="#00000000" />
</RelativeLayout>
row.xml
<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:padding="5dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="User Name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="5dp"
android:text="Address"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="16sp" />
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Edit"
android:textColor="@android:color/black" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/button1"
android:layout_marginTop="3dp"
android:background="@android:color/darker_gray"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="Delete"
android:textColor="@android:color/black" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="5dp"
android:text="Location" />
</RelativeLayout>
MainActivity.java
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity
{
 ListView userList;
 UserCustomAdapter userAdapter;
 ArrayList<User> userArray = new ArrayList<User>();
 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //Add item in arraylist  
  userArray.add(new User("HB", "Ahmedabad", "Ahmedabad"));
  userArray.add(new User("RK", "Delhi", "Delhi"));
  userArray.add(new User("HK", "Bangure", "Bangure"));
  userArray.add(new User("AB", "Aus", "AUS"));
  userArray.add(new User("Jon", "EW", "USA"));
  userArray.add(new User("Broom", "Span", "SWA"));
  userArray.add(new User("Lee", "Aus", "AUS"));
  //Set value in Adapter
  userAdapter = new UserCustomAdapter(MainActivity.this, R.layout.row,userArray);
  userList = (ListView) findViewById(R.id.listView);
  userList.setItemsCanFocus(false);
  userList.setAdapter(userAdapter);
  //List click
  userList.setOnItemClickListener(new OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View v,
     final int position, long id) {
    Log.i("List View Clicked", "**********");
    Toast.makeText(MainActivity.this,
      "List View Clicked:" + position, Toast.LENGTH_LONG)
      .show();
   }
  });
 }
}
User.xml
public class User
{
 String name;
 String address;
 String location;
 public User(String name, String address, String location)
 {
  super();
  this.name = name;
  this.address = address;
  this.location = location;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getAddress() {
  return address;
 }
 public void setAddress(String address) {
  this.address = address;
 }
 public String getLocation() {
  return location;
 }
 public void setLocation(String location) {
  this.location = location;
 } 
}
UserCustomAdapter.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class UserCustomAdapter extends ArrayAdapter<User>
{
 Context context;
 int layoutResourceId;
 ArrayList<User> data = new ArrayList<User>();
 public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data)
 {
  super(context, layoutResourceId, data);
  this.layoutResourceId = layoutResourceId;
  this.context = context;
  this.data = data;
 }
 @Override
 public View getView(int position, View convertView, ViewGroup parent)
 {
  View row = convertView;
  UserHolder holder = null;
  if (row == null)
  {
   LayoutInflater inflater = ((Activity) context).getLayoutInflater();
   row = inflater.inflate(layoutResourceId, parent, false);
   holder = new UserHolder();
   holder.textName = (TextView) row.findViewById(R.id.textView1);
   holder.textAddress = (TextView) row.findViewById(R.id.textView2);
   holder.textLocation = (TextView) row.findViewById(R.id.textView3);
   holder.btnEdit = (Button) row.findViewById(R.id.button1);
   holder.btnDelete = (Button) row.findViewById(R.id.button2);
   row.setTag(holder);
  }
  else
  {
   holder = (UserHolder) row.getTag();
  }
  User user = data.get(position);
  holder.textName.setText(user.getName());
  holder.textAddress.setText(user.getAddress());
  holder.textLocation.setText(user.getLocation());
  holder.btnEdit.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    Log.i("Edit Button Clicked", "**********");
    Toast.makeText(context, "Edit button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  holder.btnDelete.setOnClickListener(new OnClickListener()
  {
   @Override
   public void onClick(View v)
   {
    // TODO Auto-generated method stub
    Log.i("Delete Button Clicked", "**********");
    Toast.makeText(context, "Delete button Clicked",
      Toast.LENGTH_LONG).show();
   }
  });
  return row;
 }
 static class UserHolder
 {
  TextView textName;
  TextView textAddress;
  TextView textLocation;
  Button btnEdit;
  Button btnDelete;
 }
}
Note- Don't forget to add below line in Button View-
android:focusable="false"
android:focusableInTouchMode="false"
I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.
Thank you :)



 
 
Thanks it helps a lot!!
ReplyDeleteits great thanksss
ReplyDelete