get Pressed Selector as StateListDrawable - Android Graphics

Android examples for Graphics:State List Drawable

Description

get Pressed Selector as StateListDrawable

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;

public class Main {
    public static StateListDrawable getPressedSelector(Context context,
            int idPressed, int idNormal) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);/*w  w  w .ja v a2 s . c  o  m*/
        Drawable pressed = idPressed == -1 ? null : context.getResources()
                .getDrawable(idPressed);
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_pressed }, pressed);
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_focused }, pressed);
        bg.addState(new int[] {}, normal);
        return bg;
    }
}

Related Tutorials