get Selected Selector as StateListDrawable - Android Graphics

Android examples for Graphics:State List Drawable

Description

get Selected 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 getSelectedSelector(Context context,
            int idSelected, int idNormal) {
        StateListDrawable bg = new StateListDrawable();
        Drawable normal = idNormal == -1 ? null : context.getResources()
                .getDrawable(idNormal);/*from  w  w  w.j  a v a  2s. c  o  m*/
        Drawable selected = idSelected == -1 ? null : context
                .getResources().getDrawable(idSelected);
        bg.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_selected }, selected);
        bg.addState(new int[] {}, normal);
        return bg;
    }
}

Related Tutorials