Kotlin Declarative GUIs

By pjain      Published Dec. 16, 2019, 7:55 a.m. in blog Programming   

Anko - Dynamic UI basics 4US


r Anko

Anko dynamic

  • Radiobutton list view, click on row changes background color http://blog.teamtreehouse.com/making-sharedpreferences-easy-with-kotlin

    class MainActivity : AppCompatActivity() { val colors = mapOf("Black" to Color.BLACK, "Blue" to Color.BLUE, "Cyan" to Color.CYAN, "Dark Gray" to Color.DKGRAY, "Gray" to Color.GRAY, "Green" to Color.GREEN, "Light Gray" to Color.LTGRAY, "Magenta" to Color.MAGENTA, "Red" to Color.RED, "Transparent" to Color.TRANSPARENT, "White" to Color.WHITE, "Yellow" to Color.YELLOW) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val bgColor = Color.BLACK // using Anko for the layout verticalLayout { backgroundColor = bgColor radioGroup { colors.forEach { radioButton { val colorText = it.key val colorInt = it.value text = colorText textColor = Color.WHITE buttonTintList = ColorStateList.valueOf(Color.WHITE) // API 21+ onClick { selectColor(this@verticalLayout, colorInt) } } } } } } private fun selectColor(view: View, color: Int) { view.backgroundColor = color } }

DBI w Anko


There are no comments yet

Add new comment

Similar posts

SOA Api Metadata

Serverless Computing

Android Layouts Design 101

Drawing, Canvas in Kotlin