Jay Paulynice

My scratchpad where I write about coding, startups, music, weight lifting, fitness, and life in general.

Android- Change A Shape’s Color Dynamically.

This post is part of a small android jigsaw puzzle game that I have been working on for some time. I originally created the project 2 years ago when I worked on a similar python jigsaw puzzle game.

The project is on my github here: Android-Jigsaw-Puzzle

The idea is very simple: a user draws a picture then creates a jigsaw puzzle with the drawing. The application slices the image into n x m pieces which are then randomly rearranged. The user tries to reconstruct the image by dragging the slices into the correct positions.

I thought this would be a fun idea because after drawing the picture, the user would need a lot of attention to detail to recreate it from the randomly generated and rearranged pieces.

These are some example drawings:

A boat with green color selected:

Boat with orange color selected:

Looking at the lower rows, we see 6 colors and 4 brush sizes. When a user selects a color, all 4 brushes change to show the selected color.

With Android, a developer can create an image by specifying an xml resource. For example this is the small brush which resides under the Android-Jigsaw-Puzzle/app/src/main/res/drawables folder.

small.xml file:

This image can then be referenced as the source code of an image button using the @drawable/id where ‘id’ is the shape created above…@drawable/small in our case.

One problem I came across was how to dynamically change the color of the brushes when a user selects a color. I googled for a few hours and none of the solutions seemed to work for me…for example: stack overflow. Finally, I thought hard about it and came up with this solution:

It could be that the android API has changed, but this is quite simple. However, it took me a long time to get it. So I wrote this quick post in the hope that someone looking to change a shape’s color dynamically in android can find it.


  1. Hi,

    The solution for dynamically changing the color of the brush helped me a lot!! I googled for a while and couldn’t find the solution. Thanks!

  2. Jay

    March 28, 2015 at 12:32 am

    Glad it helped!

Leave a Reply

Your email address will not be published.


© 2019 Jay Paulynice