android create dynamic state-list drawables

1.2k Views Asked by At

I want android buttons with linear gradients at 90 degrees. When the button is state_pressed, the gradient flips to -90 degrees, to make it look more interactive

In my current implementation, this requires a layout XML file, two shape drawable files, and a state-list drawable file.

Working backwards, the state-list drawable contains the two shape drawable files, and the layout xml contains a button which sets its src or background to the state-list drawable.

The problem here is configuring the colors, instead of manually entering two colors for my gradients in the shape drawable files, I would like to have a button object which takes one color, and finds a different shade of that color for the other end of the gradient, sets the linear gradient's degrees, and creates multiple states for those gradients.

This way there won't be any XML file clutter and I can pass in only one color, from my colors.xml file

In any case, this would be a productive addition to an android project, this object would also increase the aesthetics of android app UI/UX

1

There are 1 best solutions below

1
On BEST ANSWER

This is a big question to answer. I could point you to links where we've done something similar. https://hg.mozilla.org/mozilla-central/file/ba6c02fc1fe6/mobile/android/base/ShapedButton.java#l107 shows a way to create StateListDrawable on the fly.

Here's a way to apply filters on a single drawable file: https://sriramramani.wordpress.com/2012/07/22/oh-my-textviews/