Kotlin ListView Button Android Tutorial With Example Click Listener

Android Kotlin ListView Button Tutorial With Example is covered in this article.

In this example, we will make a listview with button and textview in every child row of the listview.

You will learn how to manage clicklistener for every button.

There are some common problems like,

  • You have clicked the button of first row but it affects last row.
  • When you scroll down through the listview, click effect is gone

In this tutorial, we will solve above problems.

There are two buttons and one textview in every row of the listview. Textview will represent the counter(an integer number).

We will update the counter with two buttons – plus and minus.

Before you start

Make sure you are familiar with the model method to implement listview.

If you do not know about model structure for listview then read how to make model in listview example with kotlin.

When you need to put checkbox in listview then visit kotlin listview with checkbox tutorial.

Download the Source code for Kotlin ListView Button Tutorial

Step 1. Model Class

Open a new class with name Model.kt

Source code for this class contains following lines.

We will create various objects of model class and will set them in one arraylist.

This arraylist will give the data like name of the fruit and the quantity of the fruits for each row of listview.

Step 2. Creating row layout file

We need to create one layout resource file, which will represent the each row layout of the listview.

Make a file and give it a name lv_item.xml

Add below code in it.

Step 3: Integer Resource File

Now make one integer resource file named integer.xml

This file includes constant integer values with specific key names.

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

listview with button android integer, kotlin listview button

How to create integer.xml

Following is the source code for this file.

Here, btn_plus_view, btn_plus_pos etc. are the keys and 1,2 are the values for their keys.

Step 4. Preparing Custom Adapter

Custom Adapter will send data to th listview about button names and textview’s value in string format.

Open a fresh new class with name CustomAdapter.kt

Source code should look like following

For updating counter with plus and minus button clicks, we need to capture button of which row is clicked.

For this purpose, we will set tags to each button in every row of listview. Then we will retrieve position of clicked by getting those tags.

This is the main logic of this whole tutorial.

For setting tags, setTag() method is used and getTag() method will give access to clicked button.

Consider following code lines

With every minus button, view and position are tagged with unique key.

Now following code will get this tag.

As you can see, first of all we will get view of whole row layout and we store it in the tempview variable.

Then, with the help of tempview, we will get the textview and will update it’s value.

We need to update the quantity of fruits with plus and minus button click. This is done by following line

Here, modelArrayList is defined in the Main Activity and it’s access is defines as public, so we can use it in any class of whole application.

Step 5. Next Activity To Send Data

Prepare a new activity named NextActivity.

We will send data to this next activity.

It will generate two file : NextActivity.kt and activity_next.xml

Put below code into the NextActivity.kt

Now add following into the activity_next.xml

Step 6. Changing Main Activity

There are some necessary changes needed to be done in MainActivity.kt class.

Replace your existing source code with below one.

Now change your activity_main.xml file’s code with below

After following all the above steps, you should have following output for kotlin listview button example

So it was all the details about making kotlin listview button in android studio.

If you have any queries then comment it out, We will respond you quickly.

Do not forget to share DemoNut’s resources in your social media.

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

Leave a Reply

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