Javatpoint Logo
Javatpoint Logo

Kotlin Android Custom ListView

In the previous tutorial Kotlin Android ListView, we have created an example of default ListView. Android provides a facility to customize the ListView. In this tutorial, we will customize our ListView.

An Adapter class is used to add the list items in the list. It bridges the list of data between an AdapterView with other Views components (ListView, ScrollView, etc.).

Kotlin Android Custom ListView Example

In this example, we will create a custom ListView and perform click action on list items. In this custom ListView, we add one image and two different text descriptions for each row of ListView.

activity_main.xml

In the activity_main.xml file add a ListView component to display the list of items.

custom_list.xml

Create a layout file named as custom_list.xml in the layout directory and add one ImageView and two TextView. The ImageView is used to display image, one TextView to display the title and another TextView to display text description.

MainActivity.kt

Add the following code in the MainActivity.kt class. In this class, we are creating two String array and one Int array to store string text and image id respectively.

From this class, we are calling a custom adapter class named as MyListAdapter by passing the context and data as the parameters. The listView.adapter = myListAdapter set the return adapter to the ListView.

MyListAdapter.kt

Now, create a custom adapter class named as MyListAdapter.kt which populates the data model into the ListView.

Output:

Kotlin Android Custom ListView Kotlin Android Custom ListView
Next Topic#




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA