Skip to main content

Posts

Showing posts with the label ListView

android kotlin - ListView alternate row color with ripple

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.content.res.ColorStateList import android.graphics.Color import android.graphics.drawable.ColorDrawable import android.graphics.drawable.RippleDrawable import android.os.Bundle import android.view.View import android.view.ViewGroup 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 listView = findViewById<ListView>(R.id.listView) val textView = findViewById<TextView>(R.id.textView) // list to populate list view val list = mutableListOf( "Golden yellow", "Han purple", "Carrot orange", "Carnation pink", "Jungle green", ...

android kotlin - ListView delete item on click

MainActivity.kt package com.cfsuman.kotlintutorials import android.app.Activity import android.app.AlertDialog import android.os.Bundle import android.widget.* class MainActivity : Activity() { lateinit var context:MainActivity override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // get the context context = this // get the widgets reference from XML layout val listView = findViewById<ListView>(R.id.listView) val listView2 = findViewById<ListView>(R.id.listView2) // list to populate list view val list = mutableListOf( "Golden yellow", "Han purple", "Hansa yellow", "Jazzberry jam" ) // initialize an array adapter val adapter:ArrayAdapter<String> = ArrayAdapter<String>( contex...

Android Kotlin: How to programmatically disable an item in ListView

This code demonstrates two approaches to disable items in a ListView within an Android application written in Kotlin. Disabling an item prevents the user from clicking it and performing any associated actions. The code includes a button that allows the user to manually disable an item and also showcases programmatic disabling during the list adapter initialization. Breakdown of the Code The code consists of two main parts: the MainActivity.kt file containing the Activity code and the activity_main.xml file defining the layout. MainActivity.kt: This file defines the MainActivity class which extends the Activity class. It retrieves references to the UI elements (Button, ListView, TextView) from the layout using findViewById . It creates a list of color names to populate the ListView. An ArrayAdapter is initialized with the list data. This adapter provides a bridge between the data and the ListView. The isEnabled method of the adapter overrides the default behavior. It ret...