Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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)
}
over 4 years ago · Santiago Trujillo Report

0

I ran into this same problem a few months back. what I did was stored the data coming from API to one POJO class. Then in an XML file using that POJO class member as data binding variable. i.e.

<TextView
            android:layout_width="0dp"
            android:layout_height="60dp"
            android:layout_margin="@dimen/_8dp"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="@dimen/_8dp"
            app:color="@{item.color}" />

and it's binding adapter

@BindingAdapter("bind:color")
public static void setStatus(TextView textView, String color) {

}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!