50 resources in Android – why i’m lazy

So, working on this app, that has cells like this one:screen-shot-2016-09-09-at-18-35-50

See the rounded pink bar to the left? It comes in around 50 colors (it might as well by 10000000) the number is irrelevant. Before I knew it will come in different colors, I just used a png and set it as drawable (lazy, will come back to it later approach).

After I found out we’re gonna have a lot of colors, I decided that 50 PNG’s are a bad idea because they would use too much space.

So I decided I should use a 9patch for it, one for each color. Especially because it’s faster than a drawable.

Only that i’m such a lazy bastard, that copy pasting even 3 9patches was a pain. 50 is out of the question. So I decided to see if the inside of part of the 9patch can be changed. It can’t… the next idea was to use a drawable and change it programatically.

Step one: create the drawable

Pretty straight forward code, two items in a later-list, side by side. The left side has an id that I can find and alter the view programatically.

Step two: color time!

Aaaaaand it’s done.

I know it’s not much… but I’m proud of it. Something that could have taken a lot of space, now is compressed in only a few lines of code (~30).

Leave a Comment


Recent posts