Example usage for android.graphics.drawable StateListDrawable StateListDrawable

List of usage examples for android.graphics.drawable StateListDrawable StateListDrawable

Introduction

In this page you can find the example usage for android.graphics.drawable StateListDrawable StateListDrawable.

Prototype

public StateListDrawable() 

Source Link

Usage

From source file:Main.java

@NonNull
private static StateListDrawable getStateListDrawable(int normalColor, int pressedColor) {
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_focused }, new ColorDrawable(pressedColor));
    states.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(pressedColor));
    states.addState(new int[] {}, new ColorDrawable(normalColor));
    states.addState(StateSet.WILD_CARD, new ColorDrawable(normalColor));
    return states;
}

From source file:com.sbgapps.simplenumberpicker.utils.ThemeUtil.java

public static StateListDrawable makeSelector(Context context, int drawableResId, int color) {
    StateListDrawable res = new StateListDrawable();
    res.setExitFadeDuration(50);/*w ww  . ja va  2s  .c  o  m*/
    Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
    DrawableCompat.setTint(drawable, color);
    res.addState(new int[] { android.R.attr.state_enabled }, drawable);
    drawable = ContextCompat.getDrawable(context, drawableResId);
    DrawableCompat.setTint(drawable, color & 0x40FFFFFF);
    res.addState(new int[] { -android.R.attr.state_enabled }, drawable);
    return res;
}

From source file:com.justplay1.shoppist.shared.widget.ColorMultiCheckedBox.java

public void setColor(@ColorInt int color) {
    StateListDrawable states = new StateListDrawable();

    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.btn_check_to_on_000);
    Drawable checked = ContextCompat.getDrawable(getContext(), R.drawable.btn_check_to_on_015);

    if (isEnabled()) {
        states.setColorFilter(DrawableUtils.getColorFilter(color));
    } else {//from  w w  w  .j a  va2  s  . c  om
        states.setColorFilter(DrawableUtils.getColorFilter(Color.GRAY));
    }

    states.addState(new int[] { android.R.attr.stateNotNeeded }, drawable);
    states.addState(new int[] { android.R.attr.state_checked }, checked);
    states.addState(new int[] { android.R.attr.state_enabled }, drawable);
    states.addState(new int[] { -android.R.attr.state_enabled, android.R.attr.state_checked }, checked);
    states.addState(new int[] { -android.R.attr.state_enabled }, drawable);

    setButtonDrawable(states);
}

From source file:com.justplay1.shoppist.shared.widget.ColorCheckedTextView.java

private void setColor(@ColorInt int color) {
    StateListDrawable states = new StateListDrawable();

    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.btn_radio_to_on_000);
    Drawable checked = ContextCompat.getDrawable(getContext(), R.drawable.btn_radio_to_on_015);

    if (isEnabled()) {
        states.setColorFilter(DrawableUtils.getColorFilter(color));
    } else {/*from  www. j  av a2 s .c  om*/
        states.setColorFilter(DrawableUtils.getColorFilter(Color.GRAY));
    }

    states.addState(new int[] { android.R.attr.stateNotNeeded }, drawable);
    states.addState(new int[] { android.R.attr.state_checked }, checked);
    states.addState(new int[] { android.R.attr.state_enabled }, drawable);
    states.addState(new int[] { -android.R.attr.state_enabled, android.R.attr.state_checked }, checked);
    states.addState(new int[] { -android.R.attr.state_enabled }, drawable);

    setCheckMarkDrawable(states);
}

From source file:Main.java

public static Drawable createRippleDrawable(final View v, final int color, Drawable pressed) {

    Drawable drawable = v.getBackground();

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

        if (drawable instanceof RippleDrawable) {
            drawable = ((RippleDrawable) drawable).getDrawable(0);

            RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                    new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                    new int[] { color, 0 }), drawable, null);

            return rippleDrawable;
        } else {/*  ww w.j  a v  a  2  s.  c  o  m*/
            if (drawable == null) {
                drawable = new ColorDrawable(0);
                v.setBackground(drawable);
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, new ColorDrawable(0xffffffff));
                return rippleDrawable;
            } else {
                RippleDrawable rippleDrawable = new RippleDrawable(new ColorStateList(
                        new int[][] { new int[] { android.R.attr.state_pressed }, new int[] { 0 } },
                        new int[] { color, 0 }), drawable, null);
                return rippleDrawable;
            }
        }
    } else {

        if (drawable == null) {
            drawable = new ColorDrawable(color);
        }

        if (pressed == null) {
            pressed = drawable;
        }

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[] { android.R.attr.state_pressed, }, pressed);

        if (v.getBackground() != null) {
            sld.addState(new int[] { 0, }, drawable);
        }

        return sld;
    }
}

From source file:com.justplay1.shoppist.features.settings.widget.ColorCheckBoxPreference.java

@Override
protected void onBindView(View view) {
    super.onBindView(view);

    CheckBox checkboxView = (CheckBox) view.findViewById(R.id.checkbox);
    StateListDrawable states = new StateListDrawable();

    Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.btn_check_to_on_000);
    Drawable checked = ContextCompat.getDrawable(getContext(), R.drawable.btn_check_to_on_015);

    if (isEnabled()) {
        states.setColorFilter(DrawableUtils.getColorFilter(color));
    } else {//from  w  w w  . j  a  v a 2 s.  c  o  m
        states.setColorFilter(DrawableUtils.getColorFilter(Color.GRAY));
    }

    states.addState(new int[] { android.R.attr.stateNotNeeded }, drawable);
    states.addState(new int[] { android.R.attr.state_checked }, checked);
    states.addState(new int[] { android.R.attr.state_enabled }, drawable);
    states.addState(new int[] { -android.R.attr.state_enabled, android.R.attr.state_checked }, checked);
    states.addState(new int[] { -android.R.attr.state_enabled }, drawable);

    checkboxView.setButtonDrawable(states);
}

From source file:com.waz.zclient.ui.utils.ColorUtils.java

public static Drawable getButtonBackground(int borderColor, int fillColor, int strokeWidth, int cornerRadius) {
    int fillColorPressed = getPressColor(PRESSED_ALPHA, fillColor);
    int borderColorPressed = getPressColor(PRESSED_ALPHA, borderColor);

    GradientDrawable gradientDrawablePressed = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] { fillColorPressed, fillColorPressed });
    gradientDrawablePressed.setStroke(strokeWidth, borderColorPressed);
    gradientDrawablePressed.setCornerRadius(cornerRadius);

    GradientDrawable gradientDrawable = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
            new int[] { fillColor, fillColor });
    gradientDrawable.setStroke(strokeWidth, borderColor);
    gradientDrawable.setCornerRadius(cornerRadius);

    StateListDrawable states = new StateListDrawable();
    states.addState(new int[] { android.R.attr.state_pressed }, gradientDrawablePressed);
    states.addState(new int[] { android.R.attr.state_focused }, gradientDrawablePressed);
    states.addState(new int[] {}, gradientDrawable);

    return states;
}

From source file:com.redinput.datetimepickercompat.date.YearPickerView.java

/**
 * @param context/*from  w w w  .ja  v a2 s.c om*/
 */
public YearPickerView(Context context, DatePickerController controller) {
    super(context);
    mController = controller;
    mController.registerOnDateChangedListener(this);
    ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    setLayoutParams(frame);
    Resources res = context.getResources();
    mViewSize = res.getDimensionPixelOffset(R.dimen.date_picker_view_animator_height);
    mChildSize = res.getDimensionPixelOffset(R.dimen.year_label_height);
    setVerticalFadingEdgeEnabled(true);
    setFadingEdgeLength(mChildSize / 3);
    init(context);
    setOnItemClickListener(this);
    setSelector(new StateListDrawable());
    setDividerHeight(0);
    onDateChanged();
    installAccessibilityDelegate();
}

From source file:com.android.datetimepicker.date.YearPickerView.java

/**
 * @param context/*w ww  .  j  ava  2 s .  c o m*/
 */
public YearPickerView(Context context, DatePickerController controller) {
    super(context);
    mController = controller;
    mController.registerOnDateChangedListener(this);
    ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT);
    setLayoutParams(frame);
    Resources res = context.getResources();
    mViewSize = res.getDimensionPixelOffset(R.dimen.date_picker_view_animator_height);
    mChildSize = res.getDimensionPixelOffset(R.dimen.year_label_height);
    setVerticalFadingEdgeEnabled(true);
    setFadingEdgeLength(mChildSize / 3);
    init(context);
    setOnItemClickListener(this);
    setSelector(new StateListDrawable());
    setDividerHeight(0);
    onDateChanged();
}

From source file:eu.geopaparazzi.library.forms.FormListFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    List<String> fragmentTitles = mFragmentListSupporter.getListTitles();

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_list_item_activated_1,
            fragmentTitles.toArray(new String[fragmentTitles.size()]));

    int color = Compat.getColor(getActivity(), R.color.formcolor);
    int[] colors = { 0, color, 0 };
    ListView listView = getListView();
    listView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
    listView.setDividerHeight(2);//from  w  w w .ja  v a 2s .  c o  m

    listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    listView.setFocusableInTouchMode(true);
    StateListDrawable selector = new StateListDrawable();
    selector.addState(new int[] { -android.R.attr.state_pressed },
            new ColorDrawable(Compat.getColor(getContext(), R.color.main_selection)));
    listView.setSelector(selector);

    setListAdapter(adapter);

    if (fragmentTitles.size() > 0)
        listView.setItemChecked(0, true);
}