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",
            "Lavender blush",
            "Lawn green"
        )

        // initialize an array adapter
        val adapter:ArrayAdapter<String> = object: ArrayAdapter<String>(
            this,
            android.R.layout.simple_dropdown_item_1line,
            list
        ){
            override fun getView(position: Int, convertView: View?,
                                 parent: ViewGroup): View {

                val tv =  super.getView(position,
                    convertView, parent) as TextView

                // list view item alternate color
                if (position % 2 == 0){
                    tv.background = getRippleDrawable()
                }else{
                    tv.background = getRippleDrawable(
                        rippleColor = Color.parseColor("#E30022"),
                        contentColor = Color.parseColor("#FEFEFA")
                    )
                }

                return tv
            }
        }


        // attach the array adapter with list view
        listView.adapter = adapter


        // list view item click listener
        listView.onItemClickListener = AdapterView.OnItemClickListener {
                parent, view, position, id ->

            val clickedItem = parent?.getItemAtPosition(position)
                .toString()
            textView.text = "Clicked : $clickedItem"
        }
    }
}



// method to generate ripple drawable
fun getRippleDrawable(
    rippleColor: Int = Color.parseColor("#007FFF"),
    contentColor: Int = Color.parseColor("#F0FFFF"),
    maskColor: Int = Color.RED
): RippleDrawable {
    return RippleDrawable(
        ColorStateList.valueOf(rippleColor),
        ColorDrawable(contentColor),
        ColorDrawable(maskColor)
    )
}
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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC"
    android:padding="24dp">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:text="TextView" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android examples