Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write binding adapter to set background colour when the colour is coming from api as hex code?

Tags:

android

kotlin

Background colour is coming from API in this form FFF9E6.I am using data binding.I am not able to understand how I can set it as background colour. I believe binding adapter would work but not sure how to write it. How can I write binding adapter for the same?

like image 470
krai29 Avatar asked Nov 06 '25 22:11

krai29


1 Answers

This code worked in my case.

@BindingAdapter("android:backgroundColor")
fun ViewGroup.setBackground(backgroundColor: String) {

val color: Int = try {
    Color.parseColor(background)
} catch (e: Exception) {
    Color.parseColor("#$background")
}
setBackgroundColor(color)
}
like image 197
krai29 Avatar answered Nov 08 '25 12:11

krai29



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!