Working with Android Palette API

SaveSavedRemoved 1
Deal Score+5
Deal Score+5

What is Palette API in Android?

An Android Palette API is a library given by Google to extract color from any of the images used in our application and use the extracted color in that application.

Why use Android Palette API?

Mostly it depends on the design and architecture of an application. Sometimes we need to match the page background color to some images. For example, if we have a flower image in a page and the background is a single color like full black or white or any single color, then it might look ok, but not perfect. If we give a perfect lighter color to the background which will be matched to the image, then obviously the whole page will look a little different and beautiful. So, we can set a background color to match the image when we know the image. But what, if the image is dynamically downloaded from the web? So here the Android Palette API will help us by extracting some colors from that image which we can use anywhere in the page (Activity or Fragment) like background color, toolbar color and any view color like button, textbox, etc. The palette API can able to extract some specific colors provided by you. The specific colors are Vibrant, Vibrant Dark, Vibrant Muted, Muted, Muted Dark, and Muted Light. I have pasted one image on top of this page. It has all these specific color names and associated colors as well for our reference. But if we don’t want any of these specific colors, then this API has 15 colors by default and I think it’s more than enough. How to add Palette API to Android: I hope we all have Android Studio. Let’s open the application level build file and add the Palette dependency inside our dependencies. Let me know in comments, if you are facing any issues, I will share pictures here. Just like below.   And you are ready to go. Just add a colorful ImageView with any Image and six Buttons in your XML file. We will extract each color and add to each Button’s background. Let’s create the XML file and name it as main_layout.xml. Just like below.   Now let’s discuss the Java code a little. Ways to Extract Colors From Palette API:

Mostly, there are two ways to extract colors from Palette API.

  • Synchronous Way
  • Asynchronous Way
Both are pretty easy to do. Let’s see. Synchronous Way: As we know Synchronous means One By One. That means if there and many tasks, then each task has to wait until the current task to finish. Just like our bathroom. One can at a time and others have to wait. So if we want to use a Synchronous way to extract the colors, then we only have to pass the Bitmap of the current Image as a parameter in generate) method, which is a default method to extract the colors. Like below: Palette p = Palette.from(bitmap).generate(); Asynchronous Way: Asynchronous means many at a time. That means, the capability to do multiple tasks at a time. Here we have to pass both the Bitmap and one Listener as a parameter. The listener will work like a callback (i.e return data to the same class from which it started) once the work is done. Just like below:
 Palette.from(bitmap).generate(new PaletteAsyncListener() {
    public void onGenerated(Palette p) {
      // Use Palette here

Getting the Swatch from the Palette:

Once we have the Palette Object, then we can get each color swatches from it and use further as each button background. Then I have written the Java class to understand properly. Just have a look.   After this code, we will get something like below. Working with Android Palette API

We will be happy to hear your thoughts

Leave a reply

Gsmprix | Mobile Phone Reviews, Mobile Specifications
Compare items
  • Total (0)