On several occasions I had to create simple buttons that use different background color than the default gray. I've tried to google for already cooked solution but quite surprisingly -- I was unable to find one that suits me so I had to create solution by my self (not really, I just combined other solutions in one easy to use packaged solution).
There are three approaches:
1. First approach (bad) -- just set the background to some color like this
The problems:
a) button does not have rounded edges (and does not fit in the look & feel, i.e. looks alien)
b) does not support changes of the color to indicate focus or pressed state.
2. Second approach (not very good) -- create custom drawable definitions in xml file that use gradients (<gradient>). The problem is that buttons don't look quite the same as default buttons -- edges are not quite with the same roundness, margins and paddings are different. When I first tried to create colored buttons I've tried this approach but after 2-3 hours playing with paddings and roundess I finally gived up.
3. Third approach (the right(eous) one) -- Use different 9-patch images for the different colors.
It works very simple: (more…)