Listview With Button Android Studio Example

Hello, In this listview with button android tutorial, you will learn how to use a button in listview item and how to handle it clicks listeners.

First of all check out the output of this example, so that you can have an idea what we are developing at the end of the tutorial.

Wait for one minute

If you have not basic idea about listview and adapter, check out listview with image and text tutorial first.

Check listview with checkbox android example for gaining knowledge about how to use a checkbox in listview row.

Download Source Code for listview with button android tutorial

Creating Listview With Button Android Example

Step 1: Create a new project in the android studio.

Step 2: Updating activity_main.xml file

Add following code into activity_main.xml file

Step 3: Creating class

Create a new class named and add following

Step 4: Creating Integer Resource File

In this tutorial, we need to create integer resources.

Integer resources are same as string resources which are written at string.xml file in values directory.

To define integer resources, we need to create integer.xml file in values directory.

To create integer.xml file, right click on values folder -> New -> Value resource file

listview with button android integer

Directory for integer.xml

Copy following code in an integer.xml file

Step 5: Creating row layout for listview

Create one layout resource file and name it as lv_item.xml and copy below code

Step 6: Creating class:

Create new java class named CustomAdapter and add following code

Step 7: Setting tag for position and convertView

Look at this code

When listview is populating, we can set tags for postion and views as per above code.

We have set tags on btn_plus for position and converView.

setTag(key,value) method is used for this.

key for tag is used from the integer.xml file, we created earlier.

Your key should be unique for setting each tag.

Step 8: Getting tags in buttons onClick() method

You can get position and converView by using getTag(key) method.

We are setting tempview with view got from getTag() method.

In getTag() method we are using same key, which we used in setTag() method.

Similarly we can get position also using getTag() method and proper key.

Here tempview is a whole row view which contains two textView and two button.

You can inflate views of row view and set values to them. Here we are setting textView’s value by following code

Similarly, for btn_minus, all tags are set and get and values of textView are updated.

Step 9: Creating NextActivity

To open new activity in android studio, click on File tab which is present at the left top bar.

Now go to File->New->Activity->Empty Activity and give name of activity as NextActivity.

listview with button android next activity

Step 10: Updating NextActivtiy code

Add following code into activity_next.xml 

Add following into class

Step 11: Updating class

Put below code in

So it was all about implementing listview with button android feature in our android app programmatically. Thank you and keep coding!

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

6 Responses

  1. andy says:

    Can I use setTag() method for setting tag for every individual view of row item?

    • Hardik Parsania says:

      Yes you can do this but it is advisable to set tag for whole view and then inflate sub views as per requirements.

  2. Wiley says:

    Hi there I am so grateful I found your web site, I really found you by error, while I was browsing on Digg for something else, Anyways I am here now and would just like to say kudos for a remarkable post and a all round thrilling blog (I also love the theme/design), I don’t have time to go through it all at the minute but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the excellent job.

  3. Nikhil agarwal says:

    What should i do if i want to send selected item in next activity

Leave a Reply

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