Home » Android » listview – Set OnClick Listener on button inside list view in android

listview – Set OnClick Listener on button inside list view in android

Posted by: admin June 15, 2020 Leave a comment


I have a list view in my screen, every list item contains 2 text views and one button.
On button click i want to take the list item selected index to get some data from a vector.

This is my List Custom Adapter.But i don’t know how to do that.

private class CustomAdapter extends ArrayAdapter<ServicesItems> {

    public CustomAdapter(Context context, int resource,
            int textViewResourceId, List<ServicesItems> objects) {
        super(context, resource, textViewResourceId, objects);

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        TextView item = null;
        TextView description = null;
        Button subNowBtn;

        ServicesItems ii = getItem(position);

        if (null == convertView) {
            convertView = mInflater.inflate(
                    R.layout.list_of_servics_item_2, null);

            holder = new ViewHolder(convertView);
        holder = (ViewHolder) convertView.getTag();
        // cat_name = holder.gettitle();

        Resources resources = getResources();

        ServicesItems bean1 = (ServicesItems) servicesVector

        String cat_name_str = bean1.getService_name().toString();
        String descreption = bean1.getDescription().toString();

        item = holder.getItem();

        description = holder.getDescription();


        return convertView;

    private class ViewHolder {
        private View mRow;
        private TextView description = null;
        private TextView item = null;
        private Button sub = null;

        public ViewHolder(View row) {
            mRow = row;

        public TextView getDescription() {
            if (null == description) {
                description = (TextView) mRow
            return description;

        public TextView getItem() {
            if (null == item) {
                item = (TextView)       
            return item;

        public Button getSubButton(){

            return sub;


How to&Answers:

In your Adapter try this:

public View getView(final int position, View convertView, ViewGroup parent)
    View row = convertView;
    YourWrapper wrapper = null;

    if (row == null)
        row = inflater.inflate(R.layout.layout, parent, false);
        wrapper = new YourWrapper (row);
        wrapper = (YourWrapper) row.getTag();

    wrapper.getButton().setOnClickListener(new OnClickListener()
        public void onClick(View v)
            // What you want

    return row;


Your wrapper class:

public class YourWrapper
    private View base;
    private Button button;

    public YourWrapper(View base)
        this.base = base;

    public Button getButton()
        if (button == null)
            button = (Button) base.findViewById(R.id.your_button);
        return (button`);

Change sub=(Button)findViewById(R.id.subscribe_now_btn); into sub=(Button) mRow.findViewById(R.id.subscribe_now_btn);


You can get the index of list-view on button click, here are two examples:–

You can write these in your onClick method of listener.

Example 1

View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);

Example 2

Set position using setTag when creating view


Get position in the listener

int position = (Integer) view.getTag();

Hope this help 🙂