DisplayName
DisplayName

Reputation: 249

Kotlin: how to get a background's color from Button class?

I want to get Int value of background's color of a button. I try in this way

originalColorOfButton = (button.background.mutate() as ColorDrawable).color

but I get an exception of Java RunTime:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.LoginActivity}: java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable

How can I get that?

Upvotes: 0

Views: 70

Answers (1)

kitfist0
kitfist0

Reputation: 109

Java:

int backgroundColor = ((ColorDrawable)view.getBackground()).getColor();

Kotlin:

val backgroundColor = (view.background as ColorDrawable).color

Upvotes: 1

Related Questions