Android : How to set onClick event for Button in List item of ListView
I want to add onClick
event for buttons used in item of Listview
.
How can I give onClick
event for buttons in List Item.
Solution 1:
In Adapter Class
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.vehicals_details_row, parent, false);
Button deleteImageView = (Button) row.findViewById(R.id.DeleteImageView);
deleteImageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//...
}
});
}
But you can get an issue - listView row not clickable. Solution:
- make ListView focusable
android:focusable="true"
- Button not focusable
android:focusable="false"
Solution 2:
You can set the onClick
event in your custom adapter's getView
method..
check the link http://androidforbeginners.blogspot.it/2010/03/clicking-buttons-in-listview-row.html
Solution 3:
Try This,
public View getView(final int position, View convertView,ViewGroup parent)
{
if(convertView == null)
{
LayoutInflater inflater = getLayoutInflater();
convertView = (LinearLayout)inflater.inflate(R.layout.YOUR_LAYOUT, null);
}
Button Button1= (Button) convertView .findViewById(R.id.BUTTON1_ID);
Button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// Your code that you want to execute on this button click
}
});
return convertView ;
}
It may help you....
Solution 4:
In your custom adapter inside getView method :
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Do things Here
}
});
Solution 5:
This has been discussed in many posts but still I could not figure out a solution with:
android:focusable="false"
android:focusableInTouchMode="false"
android:focusableInTouchMode="false"
Below solution will work with any of the ui components : Button, ImageButtons, ImageView, Textview. LinearLayout, RelativeLayout clicks inside a listview cell and also will respond to onItemClick:
Adapter class - getview():
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.my_ref_row, parent, false);
}
final Organization currentOrg = organizationlist.get(position).getOrganization();
TextView name = (TextView) view.findViewById(R.id.name);
Button btn = (Button) view.findViewById(R.id.btn_check);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
context.doSelection(currentOrg);
}
});
if(currentOrg.isSelected()){
btn.setBackgroundResource(R.drawable.sub_search_tick);
}else{
btn.setBackgroundResource(R.drawable.sub_search_tick_box);
}
}
In this was you can get the button clicked object to the activity. (Specially when you want the button to act as a check box with selected and non-selected states):
public void doSelection(Organization currentOrg) {
Log.e("Btn clicked ", currentOrg.getOrgName());
if (currentOrg.isSelected() == false) {
currentOrg.setSelected(true);
} else {
currentOrg.setSelected(false);
}
adapter.notifyDataSetChanged();
}