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.

We will see how to handle button’s click listeners in every row item of listview.

I have created listview elements with multiple clickable buttons in this tutorial example.

There are some problems like

  • You click on first button, but system clicks on last button
  • Quantity number of item change when scroll the listview.

Implementing button in the listview is a little complex task.

But you can do it smoothly if you follow proper method with the efficient code snippets.

Getting the proper position of clicked button and set onclick event for button required some additional coding lines.

I will go through all these problems and teach you how to solve them easily.

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.

Creating Listview With Button Android Example

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

Empty Activity is the option, you need to select when creating 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

This class represents the various methods to set and get the data in the adapter class.

Objects of this class will be used to maintain the proper data structure.

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

This layout file says us how every row of the listview will be look a like.

  • Mainly two textviews and two buttons are present in this file. One button will increase the fruit quantity and the other one decrease it.
  • One textview represnts the name of the fruit.
  • Other textview shows the quantity of that fruit in terms of numbers.

Step 6: Creating class:

Create new java class named CustomAdapter and add following code

  • We are using an arraylist which is defined in the Main Activity.
  • This arraylist gives the data information to populate the listview.
  • Every object of the arraylist have value for fruit name and the quantity number.
  • Quantity number is changing with respect to the click event of plus and minus buttons.

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

Consider the below code

getModel() method creates an arraylist with the objects of the Model class.

This arraylist works as a data provider for the listview adapter.

Number of objects in the arraylist is equals to the number of rows in the listview.

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

Download Source Code for listview with button android tutorial

[sociallocker] Download Demo [/sociallocker]

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...

10 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

  4. Muhammed Ali says:

    Thank you for your valuable information

  5. Noor Akbar says:

    your code cannot be run on another studio. some thing is missing there while uploading.

Leave a Reply

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