how to create a listview with custom adapter in kotlin android
In this Article Today Learn how to create a listview with custom adapter in kotlin android. follow the Full Source Code how to create a listview with custom adapter in kotlin android.
1.MainActivity.kt:
package com.example.custometutorial;
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 language = arrayOf<String>("C","C++","Java",".Net","Kotlin","Ruby","Rails","Python","Java Script","Php","Ajax")
val description = arrayOf<String>(
"C programming is considered as the base for other programming languages",
"C++ is an object-oriented programming language.",
"Java is a programming language and a platform.",
".NET is a framework which is used to develop software applications.",
"Kotlin is a open-source programming language, used to develop Android apps and much more.",
"Ruby is an open-source and fully object-oriented programming language.",
"Ruby on Rails is a server-side web application development framework written in Ruby language.",
"Python is interpreted scripting and object-oriented programming language.",
"JavaScript is an object-based scripting language.",
"PHP is an interpreted language, i.e., there is no need for compilation.",
"AJAX allows you to send and receive data asynchronously without reloading the web page."
)
val imageId = arrayOf<Int>(
R.drawable.c_image,R.drawable.cpp_image,R.drawable.java_image,
R.drawable.net_image,R.drawable.kotlin_image,R.drawable.ruby_image,
R.drawable.rails_image,R.drawable.python_image,R.drawable.js_image,
R.drawable.php_image,R.drawable.ajax_image,R.drawable.python_image,
R.drawable.hadoop_image
)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myListAdapter = MyListAdapter(this,language,description,imageId)
listView.adapter = myListAdapter
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.MyListAdapter.kt:
package com.example.custometutorial;
import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.*
class MyListAdapter(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.example.custometutorial" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Wonderful Post!!! Thanks for sharing this great blog with us.
ReplyDeleteDot Net framework Overview
What is Dot Net Framework
This blog contains more useful information about the topic, thanks for this blog.
ReplyDeleteAdvanced Excel Course in Chennai
Advanced Excel Training Online