- number_circle.xml
- 로또 숫자를 나타내는 TextView가 backgound로 사용하는 drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="@color/number_color1"/>
<size
android:width="40dp"
android:height="40dp"/>
</shape>
- MainActivity.kt
- 로또 숫자가 해당하는 범위에 따라 drawable의 색을 바꾸는 코드
- TextView.background로 drawable 접근 -> GradientDrawable로 형변횐 -> setColor(색 ID)로 색 바꾸기
private fun updateUI(){
for(i in 0 until pickedNumbers.size){
val num = pickedNumbers[i]
val tv = numberTextViewArray[i]
tv.text = num.toString()
tv.isGone = false
var circleColorId = when(num) {
in 0..10 -> R.color.number_color1
in 11..20 -> R.color.number_color2
in 21..30 -> R.color.number_color3
in 31..40 -> R.color.number_color4
else -> R.color.number_color5
}
var shapeDrawable: GradientDrawable = tv.background as GradientDrawable
shapeDrawable.setColor(resources.getColor(circleColorId))
tv.background = shapeDrawable
}
}