Java tutorial
//package com.java2s; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; public class Main { /** * To state list drawable state list drawable. * * @param normal the normal * @param pressed the pressed * @param focused the focused * @param unable the unable * @return the state list drawable */ 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; } /** * To state list drawable state list drawable. * * @param normalColor the normal color * @param pressedColor the pressed color * @param focusedColor the focused color * @param unableColor the unable color * @return the state list 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; } /** * To state list drawable state list drawable. * * @param normal the normal * @param pressed the pressed * @return the state list drawable */ public static StateListDrawable toStateListDrawable(Drawable normal, Drawable pressed) { return toStateListDrawable(normal, pressed, pressed, normal); } /** * To state list drawable state list drawable. * * @param normalColor the normal color * @param pressedColor the pressed color * @return the state list drawable */ public static StateListDrawable toStateListDrawable(int normalColor, int pressedColor) { return toStateListDrawable(normalColor, pressedColor, pressedColor, normalColor); } }