Monday, March 2, 2020

android kotlin - Spinner item separator programmatically

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.LayerDrawable
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.*


class MainActivity : Activity() {
    private 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 spinner = findViewById<Spinner>(R.id.spinner)


        // list of spinner items
        val list = mutableListOf(
            "Amaranth red",
            "Black Shadows",
            "Bitter lemon",
            "Bright maroon",
            "Blue yonder",
            "Cadmium red"
        )


        // initialize an array adapter for spinner
        val adapter:ArrayAdapter<String> = object: ArrayAdapter<String>(
            context,
            android.R.layout.simple_spinner_dropdown_item,
            list
        ){
            override fun getDropDownView(
                position: Int,
                convertView: View?,
                parent: ViewGroup
            ): View {
                val view:TextView = super.getDropDownView(
                    position,
                    convertView,
                    parent
                ) as TextView

                // set item text bold and sans serif font
                view.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD)

                // set spinner item padding
                view.setPadding(
                    25.toDp(context), // left
                    10.toDp(context), // top
                    50.toDp(context), // right
                    10.toDp(context) // bottom
                )

                // draw bottom border of items exclude last item
                if (position != list.size-1){
                    view.background = generateBottomBorder()
                }

                return view
            }
        }

        // finally, data bind spinner with adapter
        spinner.adapter = adapter
    }
}



// extension method to convert values to dp
fun Int.toDp(context: Context):Int = TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP,
    this.toFloat(),
    context.resources.displayMetrics
).toInt()



// custom method to generate bottom border
fun generateBottomBorder(
    bgColor:Int = Color.WHITE,
    borderColor:Int = Color.GRAY,
    widthInPixels:Int= 4
):LayerDrawable{
    val drawables = arrayOf(
        ColorDrawable(borderColor),
        ColorDrawable(bgColor)
    )
    val layerDrawable = LayerDrawable(drawables)

    layerDrawable.setLayerInset(
        1, // index of drawable, background
        0, // left border in pixels
        0, // top border in pixels
        0, // right border in pixels
        widthInPixels // bottom border in pixels
    )

    return layerDrawable
}
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="64dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>