Home > Back-end >  Only change left drawable in kotlin
Only change left drawable in kotlin

Time:02-21

Is there a way of only changing the left drawable in kotlin using code? In my case I have a speedmode selector from 1 to 8 and when a car(s) is selected you then press a speedmode and the icon changes in the button of the car. I have this but it removes the right drawable.

 for (speedModeSelecter in speedList)
    {
        speedModeSelecter.setOnClickListener{
            for(kartBox in kartList)
            {
                if(kartBox.isChecked) {
                   kartBox.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.ic_speedmode1, 0 ,0 ,0)
                }
            }
        }
    }

a picture of what it should look like here

CodePudding user response:

For Kotlin

var leftDrawable: Drawable =
    getContext()
        .getResources()
        .getDrawable(R.drawable.yourdrawable)

var drawables: Array<Drawable> = button.compoundDrawables

//setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
button.setCompoundDrawablesWithIntrinsicBounds(leftDrawable, drawables[1], drawables[2], drawables[3])
  • Related