android kotlin listview example

In this article, today learn android kotlin listview example. Listview is a set of array adapter. for the more information visit google developer listview

Android ListView displays data vertically with each item positioned below the previous data items. ListView is scrollable. You can use ListView without any issues for displaying a few data items in UI. But for displaying large volumes of data as a list, you need to use to build well-performing apps.      



1.MainActivity.kt:


import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.widget.Toast  
import kotlinx.android.synthetic.main.activity_main.*  
  
  
class MainActivity : AppCompatActivity() {  
    val fruit = arrayOf<String>("Mango","cocunt","apple","Cherry","orange")  
    val description = arrayOf<String>(  
            "Mango is fruit",  
            "cocunt is fruit",  
            "apple is fruit",  
            "Cherry is fruit",  
            "orange is fruit",  
            
    )  
  
    val imageId = arrayOf<Int>(  
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,  
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,  
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,  
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,  
            R.drawable.ic_launcher  
    )  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
  
        val ListAdapter = ListAdapter(this,fruit,description,imageId)  
        listView.adapter = ListAdapter  
  
        listView.setOnItemClickListener(){adapterView, view, position, id ->  
            val itemAtPos = adapterView.getItemAtPosition(position)  
            val itemIdAtPos = adapterView.getItemIdAtPosition(position)  
            Toast.makeText(this, "Click on item at $itemAtPos its item id $itemIdAtPos", Toast.LENGTH_LONG).show()  
        }  
    }  
}  




2.activity_main.xml:


<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout 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"/>  
</android.support.constraint.ConstraintLayout>  




3.ListAdapter.kt:


import android.app.Activity  
import android.view.View  
import android.view.ViewGroup  
import android.widget.*  
class ListAdapter(private val context: Activity, private val title: Array<String>, private val description: Array<String>, private val imgid: Array<Int>)  
    : ArrayAdapter<String>(context, R.layout.custom_list, title) {  
  
    override fun getView(position: Int, view: View?, parent: ViewGroup): View {  
        val inflater = context.layoutInflater  
        val rowView = inflater.inflate(R.layout.custom_list, null, true)  
  
        val titleText = rowView.findViewById(R.id.title) as TextView  
        val imageView = rowView.findViewById(R.id.icon) as ImageView  
        val subtitleText = rowView.findViewById(R.id.description) as TextView  
  
        titleText.text = title[position]  
        imageView.setImageResource(imgid[position])  
        subtitleText.text = description[position]  
  
        return rowView  
    }  
}  



4.custom_list.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"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:orientation="horizontal">  
    <LinearLayout  
        android:orientation="horizontal"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:gravity="center" >  
    <ImageView  
        android:id="@+id/icon"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        app:srcCompat="@mipmap/ic_launcher" />  
  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_weight="1"  
        android:orientation="vertical">  
  
        <TextView  
            android:id="@+id/title"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="title"  
            android:textStyle="bold"  
            android:layout_marginLeft="15dp"  
            android:layout_marginStart="15dp"  
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />  
  
        <TextView  
            android:id="@+id/description"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:text="description"  
            android:layout_marginLeft="15dp"  
            android:layout_marginStart="15dp"  
            android:layout_marginTop="5dp"  
            android:textSize="16sp"/>  
    </LinearLayout>  
    </LinearLayout>  
</LinearLayout>  



5.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.listview">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Comments

Popular posts from this blog

Kotlin camera intent example

android kotlin current date example