Provides a backwards compatable way to tint drawables - Android Graphics

Android examples for Graphics:Drawable

Description

Provides a backwards compatable way to tint drawables

Demo Code


import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.view.View;

public class Main {
  /**// ww w  .  j av  a2 s. c o m
   * Provides a backwards compatable way to tint drawables
   * 
   * @param view
   *          the view who's background drawable will be tinted
   * @param color
   *          the color that will be applied
   */
  public static void tintViewDrawable(View view, int color) {
    final Drawable originalDrawable = view.getBackground();
    final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable);
    DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(color));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
      view.setBackground(wrappedDrawable);
    } else {
      view.setBackgroundDrawable(wrappedDrawable);
    }
  }
}

Related Tutorials