android kotlin listview onclick example
In this article today learn android kotlin listview
onclick example. Previous tutorial simple kotline listview example follow the
link: android studio kotlin listview example.
Android kotlin ListView is
a view that contains the group of items and displays in a scrollable list.
ListView is implemented by importing android.widget.ListView class.
Kotlin ListView is a default scrollable which does not use other scroll views.
ListView uses
Adapter classes that add the content from a data source (such as string array,
array, database, etc) to ListView. Adapter bridges data between AdapterViews and
other Views (ListView, ScrollView, etc).
1.MainActivity.kt:
package com.example.listview;
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
class MainActivity:AppCompatActivity() {
internal var
listView:ListView
internal var
textView:TextView
internal var
listItem:Array<String>
protected fun
onCreate(savedInstanceState:Bundle) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView =
findViewById(R.id.listView) as ListView
textView =
findViewById(R.id.textView) as TextView
listItem =
getResources().getStringArray(R.array.array_technology)
val adapter =
ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, listItem)
listView.setAdapter(adapter)
listView.setOnItemClickListener(object:AdapterView.OnItemClickListener()
{
fun onItemClick(adapterView:AdapterView<*>, view:View, position:Int, l:Long) {
// TODO
Auto-generated method stub
val value =
adapter.getItem(position)
Toast.makeText(getApplicationContext(), value,
Toast.LENGTH_SHORT).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="listview.example.com.listview.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</android.support.constraint.ConstraintLayout>
3.mylist.xml:
<?xml version="1.0"
encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#4d4d4d"
/>
4.strings.xml :
<resources>
<string
name="app_name">ListView</string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>Php</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Python</item>
<item>Ajax</item>
<item>C++</item>
<item>Ruby</item>
<item>Rails</item>
<item>.Net</item>
<item>Perl</item>
</string-array>
</resources>
5.AndroidManifest.xml:
<?xml version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.listview" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.listview.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>
No comments:
Post a Comment