ListView using Array from strings.xml with Item Click Listener kotlin

How to create a ListView from array in strings.xml in Kotlin using Android Studio?

In this tutorial we will create a ListView with Item Click Listener, we will write string array in strings.xml. When a list item is clicked a Toast will be displayed, you can change the functionality.

Step 1: Create a new project OR Open your project

Step 2: Code

strings.xml

<resources>
    <string name="app_name">Kotlin Practice</string>

    <string-array name="arrayelements">
        <item>Item 1</item>
        <item>Item 2</item>
        <item>Item 3</item>
        <item>Item 4</item>
        <item>Item 5</item>
    </string-array>
</resources>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

ActivityMain.kt

package com.jigopost.kotlinpractice

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val mListView = findViewById<ListView>(R.id.listView)

        mListView.adapter = ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1,resources.getStringArray(R.array.arrayelements))

        mListView.setOnItemClickListener{parent, view, position, id ->

            //Toast.makeText(this@MainActivity, "You have Clicked " + parent.getItemAtPosition(position), Toast.LENGTH_SHORT).show()

            if (position==0){
                Toast.makeText(this@MainActivity, "Item One",   Toast.LENGTH_SHORT).show()
            }
            if (position==1){
                Toast.makeText(this@MainActivity, "Item Two",   Toast.LENGTH_SHORT).show()
            }
            if (position==2){
                Toast.makeText(this@MainActivity, "Item Three", Toast.LENGTH_SHORT).show()
            }
            if (position==3){
                Toast.makeText(this@MainActivity, "Item Four",  Toast.LENGTH_SHORT).show()
            }
            if (position==4){
                Toast.makeText(this@MainActivity, "Item Five",  Toast.LENGTH_SHORT).show()
            }
        }
    }

}

Step 3: Output

Similar Posts

  • Options Menu Fragment – Android Studio – Kotlin

    Add setHasOptionsMenu(true) in the onCreate() method to invoke the menu items in your Fragment class. You don’t need to override onCreateOptionsMenu in your Fragment class again.  Menu items can be changed (Add/remove) by overriding onPrepareOptionsMenu method available in Fragment. This will cover the followings: Use Options Menu / Actionbar menu Use Options Menu in fragments Add items with icons in options menu Show…

  • Android Bottom Sheet in Kotlin

    This tutorial is about: How to create bottom sheet using Fragment How to handle bottom sheet item/option clicks Step 1: Create a new Project or open new project Step 2: Add following library in build.gradle(Module:app)            implementation ‘com.android.support:design:27.1.1’ Step 3: Create a fragment name it as “BottomSheetEx”. Step 4: Code build.gradle(Module:app) BottomSheetEx.kt fragment_bottom_sheet_ex.xml activity_main.xml MainActivity.kt Step 5:Run Project Output

  • Country Code Picker with Kotlin

    Country code pickers are used to search and select country code or international phone code in android. This example describes how to add a country code picker using Kotlin for Android application development. First, we needed to add the following dependency into our applications app level build.gradle file. implementation ‘com.hbb20:ccp:2.2.2’ Add the following code into your activity_main.xml file  To hide flagapp:ccp_showFlag=”false” To…

  • Swipe Refresh Layout example kotlin in android

    Pull to Swipe Refresh Layout is a gesture that is being used in a lot of popular apps, apps where content updates are frequent, like news, social media e.t.c. In Android development this gesture it’s called ‘Swipe to Refresh‘ Methods are: – setOnRefreshListener(OnRefreshListener): adds a listener to let other parts of the code know when refreshing begins.– setRefreshing(boolean): enables or disables progress visibility.– isRefreshing(): checks…

  • SharedPreferences in Kotlin Android Studio

    SharedPreferences | Android Studio | Kotlin SharedPreferences is one of the types of saving data in Android Devices. You can save String, int, boolean, long, float, and Set<String>  types of data in SharedPreferences. In SharedPreferences data is saved in the key-value form. If you have a relatively small collection of key-values that you’d like to save, then you should use the SharedPreferences APIs. You can Add, Edit/Modify and Remove data from the SharedPreferences easily.To…

Leave a Reply

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