MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.widget.*
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// get the widgets reference from XML layout
val button = findViewById<Button>(R.id.button)
val listView = findViewById<ListView>(R.id.listView)
val listView2 = findViewById<ListView>(R.id.listView2)
// list to populate list view
val list = mutableListOf(
"Black bean",
"Black coffee",
"Black chocolate"
)
// initialize an array adapter
val adapter:ArrayAdapter<String> = ArrayAdapter(
this,
android.R.layout.simple_dropdown_item_1line,list
)
// attach the array adapter with list view
listView.adapter = adapter
listView2.adapter = adapter
// remove list view divider programmatically
button.setOnClickListener {
// disable the button itself
it.isEnabled = false
// remove the list view divider
listView.divider = null
listView.dividerHeight = 0
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:id="@+id/rootLayout"
android:background="#DCDCDC"
android:padding="24dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remove Divider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#F8F8F8"
android:choiceMode="singleChoice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:choiceMode="singleChoice"
android:divider="@null"
android:dividerHeight="0dp"
android:background="#F0F8FF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/listView" />
</androidx.constraintlayout.widget.ConstraintLayout>


- android kotlin - Menu item text color
- android kotlin - Toolbar menu example
- android kotlin - Toolbar back button color
- android kotlin - Toolbar center align title
- android kotlin - Secondary progress bar example
- android kotlin - Circular progress bar example
- android kotlin - Custom horizontal progress bar
- android kotlin - Progressbar with percentage example
- android kotlin - Change ProgressBar color programmatically
- android kotlin - ListView alternate row color with ripple
- android kotlin - Create ColorStateList programmatically
- android kotlin - ListView remove item programmatically
- android kotlin - ListView add item programmatically
- android kotlin - ListView add header programmatically
- android kotlin - ListView OnItemClickListener example