Skip to main content

Posts

Showing posts from March, 2020

android kotlin - selectableItemBackground programmatically

MainActivity.kt package com.example.jetpack import android.app.Activity import android.os.Bundle import android.util.TypedValue import android.view.View import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // make the text view clickable materialTextView.apply { isClickable = true isFocusable = true } applySelectableItemBackground(materialTextView) } } // extension function to apply selectable item background fun Activity.applySelectableItemBackground(v:View){ val typedValue = TypedValue() this.theme.resolveAttribute( android.R.attr.selectableItemBackground, typedValue, true ) if (typedValue.resourceId != 0) { v.setBackgroundResour...

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 - Create ColorStateList programmatically

MainActivity.kt package com.example.jetpack import android.content.res.ColorStateList import android.graphics.Color import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // generate background tint list for button button2.backgroundTintList = generateColorStateList() // modify disabled color on method button3.backgroundTintList = generateColorStateList( // disabled color Alabaster disabledColor = Color.parseColor("#EDEAE0") ) // background tint list for checkbox and radio button materialCheckBox1.buttonTintList = generateColorStateList() materialRadioButton.buttonTintList = generateColorStateList() } } // method ...

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...