How to access a list of resources via data binding (and create nice gradient backgrounds along the road)

Let’s imagine you need to provide a gradient background consisting of arbitrarily many colors to a View.

This can’t be done via regular drawable XML as a gradient within a shape can only define three colors: startColor, centerColor, endColor.

To reach our goal we will be using data binding.

We define an integer array in e.g. arrays.xml and fill it with the desired color resources:

Then we define a binding adapter to apply the colors in a gradient to the background of the View:

Now we can define the background colors in our layout XML (apply it to any View class you like):

Note, that we have to reference integer-array resources with @intArray in data binding expressions!

--

--

Founder and Senior Android Engineer. Crowdfunding addict. Trying hard to do the right thing and failing more often than not.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sven Bendel

Founder and Senior Android Engineer. Crowdfunding addict. Trying hard to do the right thing and failing more often than not.