Let’s learn how to create a list view using Kotlin in Android. But first, we’ll begin with an introduction to Kotlin.
Normally, we use Java language for creating naïve apps for Android. When you run a Java application on a virtual machine, the application will be compiled into a set of instructions called “Bytecode”. Despite, a number of languages have been introduced to run on the Java Virtual Machine (JVM) over the past years. But, the JVM looks the same for all languages while compiling the applications. Accordingly, the main idea which JVM looks for is that language features which can help developers write simpler code and overcome the loopholes of Java language. Eventually, Kotlin is one among them!
What is Kotlin?
Now, let’s dive deep into Configuring Kotlin. This section walks you through creating a list view in Android using Kotlin language.
Configuring Kotlin in Android Studio
Here is the step-by-step approach for configuring Kotlin in Android Studio:
Step 1 – Install the latest version of Android Studio
First, Download and Install New/Latest Version of Android Studio. In order to check for updates, Go to Help and select Check for updates.
Step 2 – Install the Kotlin Plugin in Android Studio.
To install Kotlin Plugin, Go to file and select settings. Just Search Plugins for Kotlin in the window of Settings as shown in the below screenshot.
Download and Install Plugin in your project/app. Restart Android Studio after completing the installation.
Step 3 – Select Kotlin under Tools, and check whether the Kotlin plugin is installed or not as shown in the below screenshot:
Step 4 – Create a new Project.
To create a new project, Go to File, select New and select the New Project as shown in the screenshot.
Now, enter all the required credentials such as Application name, Company domain, package name and project location to configure your new project.
Step 5 – Select the Empty Activity. Click on Next and Finish to end the task.
Step 6 – Next, Customize the activity by giving a unique name to it.
Now, you will get a new project in Android Studio named ‘KotlinListView’.
Step 7 – Configure project into Kotlin.
First, add Kotlin dependencies into build.gradle as shown below.
Next, go to dependencies and add two classpaths for Kotlin as shown in the below code snippet:
Add plugins as shown in the below code snippet:
apply plugin: ‘kotlin-android’
Add dependencies for Kotlin as shown in the below code snippet:
Step 8 – Add a ListView in your xml file.
Step 9 – Next, Add Custom TextView for add items in the ListView.
Step 10 – Now, Convert Java code into Kotlin.
Now, we will convert our Java code into Kotlin, Go to Code > Convert Java File to Kotlin File as shown below (Press Alt+Ctrl+Shift+k).
Once, the conversion is done. You will get the code in Kotlin as shown in the below code snippet:
Step 11 – Take a ListView onCreate() and assign it:
<b><span style="font-size: 9.0pt; color: navy;">val </span></b><span style="font-size: 9.0pt; color: black;">lv = findViewById(R.id.</span><i><span style="font-size: 9.0pt; color: #660e7a;">list</span></i><span style="font-size: 9.0pt; color: black;">) </span><b><span style="font-size: 9.0pt; color: navy;">as </span></b><span style="font-size: 9.0pt; color: black; background: #E4E4FF;">ListView</span>
Here we will use val keyword for assigning Android List
Take a List Adapter class, extend it into BaseAdapter and add values which you want to display into the Android ListView as below:
Here are some of the methods which we have taken:
- getCount() for getting a size of List from array
- getItem() for getting a position of List Item
- getItemId() for assigning an Id on particular position
- getView() for displaying array of items into List
Step 12 – Run your app on the emulator.
You will get an output as shown in the below image:
Output: A ListView of items will be displayed.
Explore the Kotlin source code for this project from Github, https://github.com/hiteshbpatel/Android_Blog_Projects/tree/master/KotlinListView
Hope this blog helped you in understanding creating ListView using Kotlin in Android with the code snippets.
Stay tuned! We are coming soon with more Kotlin-Android blogs. Happy Coding!