Android examples for Graphics:Drawable
set Background Drawable States
//package com.java2s; import android.annotation.SuppressLint; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; import android.view.View; public class Main { public static void setBackgroundDrawableStates(View view, Drawable drawable, Drawable pressedDrawable) { setBackgroundDrawableStates(view, drawable, pressedDrawable, null); }/*from w w w . java 2 s . com*/ public static void setBackgroundDrawableStates(View view, Drawable drawable, Drawable pressedDrawable, Drawable disabledDrawable) { StateListDrawable stateDrawable = makeStateDrawable(drawable, pressedDrawable, disabledDrawable); if (stateDrawable != null) { setBackgroundDrawable(view, stateDrawable); } } public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable) { return makeStateDrawable(drawable, pressedDrawable, null); } public static StateListDrawable makeStateDrawable(Drawable drawable, Drawable pressedDrawable, Drawable disabledDrawable) { boolean set = false; StateListDrawable stateDrawable = new StateListDrawable(); if (disabledDrawable != null) { set = true; stateDrawable.addState( new int[] { -android.R.attr.state_enabled }, disabledDrawable); } if (pressedDrawable != null) { set = true; stateDrawable.addState( new int[] { android.R.attr.state_pressed }, pressedDrawable); } if (drawable != null) { set = true; stateDrawable.addState(new int[0], drawable); } return set ? stateDrawable : null; } @SuppressLint("NewApi") @SuppressWarnings("deprecation") public static void setBackgroundDrawable(View view, Drawable drawable) { if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) { view.setBackgroundDrawable(drawable); } else { view.setBackground(drawable); } } }