Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; public class Main { public static StateListDrawable toStateListDrawable(Drawable normal, Drawable pressed, Drawable focused, Drawable unable) { StateListDrawable drawable = new StateListDrawable(); drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed); drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused); drawable.addState(new int[] { android.R.attr.state_enabled }, normal); drawable.addState(new int[] { android.R.attr.state_focused }, focused); drawable.addState(new int[] { android.R.attr.state_window_focused }, unable); drawable.addState(new int[] {}, normal); return drawable; } public static StateListDrawable toStateListDrawable(int normalColor, int pressedColor, int focusedColor, int unableColor) { StateListDrawable drawable = new StateListDrawable(); Drawable normal = new ColorDrawable(normalColor); Drawable pressed = new ColorDrawable(pressedColor); Drawable focused = new ColorDrawable(focusedColor); Drawable unable = new ColorDrawable(unableColor); drawable.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, pressed); drawable.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focused); drawable.addState(new int[] { android.R.attr.state_enabled }, normal); drawable.addState(new int[] { android.R.attr.state_focused }, focused); drawable.addState(new int[] { android.R.attr.state_window_focused }, unable); drawable.addState(new int[] {}, normal); return drawable; } public static StateListDrawable toStateListDrawable(Drawable normal, Drawable pressed) { return toStateListDrawable(normal, pressed, pressed, normal); } public static StateListDrawable toStateListDrawable(int normalColor, int pressedColor) { return toStateListDrawable(normalColor, pressedColor, pressedColor, normalColor); } }