3 mins

android materialdesign android-support-library


Playing with Material Design, Toolbar and Palette

Since Android L(ollipop) was presented in June at Google I/O, only two devices on market can run it officially. Nexus 6 and Nexus 9 were released a week ago, and these two guys are ready to go with most recent system from Google, but what about other devices? My Nexus 5 still tryin update everyday, but still no effects :) Hopefully, we can use some part of Material Design with devices running Android 2.1 and higher, because most of implementation was placed in android-support-v7.

I wrote simple app which displays images in grid with possibility to pick them to see them in full size with ActionBar colors based on picked image. App uses new classes: Palette and Toolbar.

It’s looks like this:

first screenshot second screenshot third screenshot fourth screenshot

Let’s code.
All dependencies we need:

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.android.support:palette-v7:21.0.0'
    compile 'com.squareup.picasso:picasso:2.4.0'

as you can see Google split android.support and put Palette as separate lib (similar to RecyclerView and CardView). I used Picasso to simple load images from web.

Next, define AppTheme with parent as Theme.AppCombat. You can also specify colorPrimary and accentColor which are used by framework to color surface background of widgets such as text fields, checkboxes etc.

<style name="AppTheme" parent="Theme.AppCompat">
  <item name="colorPrimary">@color/black</item>
<color name="black">#000000</color>

Apply style to application:

<application android:theme="@style/AppTheme" />

App has two activities. First MainActvity extends ActionBarActivity so framework will show window with ActionBar (see first screenshot with GridView). ImageActivity is just Activity, so by default window will be rendered without ActionBar. But look at other screenshots, they have an ActionBar, but wait… actually it’s Toolbar!

Toolbar is more flexible (easier to customize) standard View, so you can put it everywhere in layout, multiple times… and it still can act as ActionBar (see setActionBar)

Let’s look at the layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"




It’s simple. Nothing special here, except using of Toolbar.

Finally we can use Pallete to extract prominent colors from image and change style of Toolbar at runtime.

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final ImageView image = (ImageView) findViewById(R.id.image);
target = new Target() {
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
        Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
            public void onGenerated(Palette palette) {
                int defaultColor = getResources().getColor(R.color.black);

Look at the screenshots, I think they look pretty well and we didn’t need a graphic designer to choose correct colors ;)

See full source code at github.
See Creating Apps with Material Design.
See Material Design specification.

See this post on my personal blog.

comments powered by Disqus