Extracting Colors In Android 5.0 – “The Palette API”

Before the new update of Palette’s API in android 5.0 developers were allowed to extract colors from images.

Now with the release of Android Lollipop, the Palette class makes easy to extract prominent colors from bitmap images. These colors are very useful if you want to style other view components to match colors from your image, such as a background for the image or a text color etc.

We have two different ways to create the Palette for an image. First is to use the generate methods, which either take just a bitmap object or a bitmap with an integer specifying the number of colors the palette should generate.



generate(Bitmap, int)



These methods are synchronous and should not be called on the main thread.

But practically, sometimes it may not be possible to do this on the same thread where you load your images on. To solve this the Palette class has another set of generating methods which are asynchronous. The only difference between the two is a listener object that will be called when the Palette is ready.


generateAsync(Bitmap, PaletteAsyncListener)

generateAsync(Bitmap, int, PaletteAsyncListener)



The implementation of the theory I just explained is very simple. Let’s follow the steps to create a sample code:

Step1:) Create a new android project in your IDE and add following dependency in you gradle build file:


compile 'com.android.support:palette-v7:21.0.0'



Step2:) Write the following in main layout file:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="Palette Colors"
android:id="@+id/textView" />





Step3:) Add a png file to your drawble folder.

Step4:) Write the following into your main activity file:


package com.example.extractingcolors;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.graphics.Palette;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {

protected void onCreate(Bundle savedInstanceState) {

ImageView imageView = (ImageView)findViewById(R.id.imageView);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.colors);

Palette palette = Palette.generate(myBitmap);

Palette.PaletteAsyncListener listener = new Palette.PaletteAsyncListener() {
public void onGenerated(Palette palette) {


Palette.Swatch swatch = palette.getVibrantSwatch();
TextView titleView = (TextView)findViewById(R.id.textView);;

if (swatch != null) {

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;

public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;

return super.onOptionsItemSelected(item);



Step5:) Run for the output.


It is showing only one color extracted from the image which I have set to text view because I haven’t specified any number when I called palette.generates() method. You can create a list or array of swatches to extract more colors.

Leave a Comment: