Create new Selector StateListDrawable - Android Graphics

Android examples for Graphics:Drawable

Description

Create new Selector 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 newSelector(Context context,
            int nNormaImgId, int nDownImgId, int nFocusImgId,
            int nDisableImgId) {
        StateListDrawable selector = new StateListDrawable();
        Drawable normalDrawable = (nNormaImgId <= 0) ? null : context
                .getResources().getDrawable(nNormaImgId);
        Drawable downDrawable = (nDownImgId <= 0) ? null : context
                .getResources().getDrawable(nDownImgId);
        Drawable focusDrawable = (nFocusImgId <= 0) ? null : context
                .getResources().getDrawable(nFocusImgId);
        Drawable disableDrawable = (nDisableImgId <= 0) ? null : context
                .getResources().getDrawable(nDisableImgId);

        // View.PRESSED_ENABLED_STATE_SET
        selector.addState(new int[] { android.R.attr.state_pressed,
                android.R.attr.state_enabled }, downDrawable);

        // View.ENABLED_FOCUSED_STATE_SET
        selector.addState(new int[] { android.R.attr.state_enabled,
                android.R.attr.state_focused }, focusDrawable);

        // View.ENABLED_STATE_SET
        selector.addState(new int[] { android.R.attr.state_enabled },
                normalDrawable);//from w  w w . ja  v  a 2  s .  c o m

        // View.FOCUSED_STATE_SET
        selector.addState(new int[] { android.R.attr.state_focused },
                focusDrawable);

        // View.WINDOW_FOCUSED_STATE_SET
        selector.addState(
                new int[] { android.R.attr.state_window_focused },
                disableDrawable);

        // View.EMPTY_STATE_SET
        selector.addState(new int[] {}, normalDrawable);
        return selector;
    }
}

Related Tutorials