1

I'm having trouble understanding how to re-write the class below so that it no longer uses deprecated code. Here is my complete class.

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.widget.Button;

public class BgButtonStyle extends Button {

    public BgButtonStyle(Context context) {
        super(context);
    }

    public BgButtonStyle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public BgButtonStyle(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setBackgroundDrawable(Drawable d) {
        // Replace the original background drawable (e.g. image) with a LayerDrawable that
        // contains the original drawable.
        BgButtonStyleBackgroundDrawable layer = new BgButtonStyleBackgroundDrawable(d);
        super.setBackgroundDrawable(layer);
    }

    /**
     * The stateful LayerDrawable used by this button.
     */
    protected class BgButtonStyleBackgroundDrawable extends LayerDrawable {
        // The color filter to apply when the button is pressed
        protected ColorFilter _pressedFilter = new LightingColorFilter(Color.LTGRAY, 1);
        // Alpha value when the button is disabled
        protected int _disabledAlpha = 100;

        public BgButtonStyleBackgroundDrawable(Drawable d) {
            super(new Drawable[]{d});
        }

        @Override
        protected boolean onStateChange(int[] states) {
            boolean enabled = false;
            boolean pressed = false;

            for (int state : states) {
                if (state == android.R.attr.state_enabled)
                    enabled = true;
                else if (state == android.R.attr.state_pressed)
                    pressed = true;
            }

            mutate();
            if (enabled && pressed) {
                setColorFilter(_pressedFilter);
            } else if (!enabled) {
                setColorFilter(null);
                setAlpha(_disabledAlpha);
            } else {
                setColorFilter(null);
            }

            invalidateSelf();

            return super.onStateChange(states);
        }

        @Override
        public boolean isStateful() {
            return true;
        }
    }
}

I use this class for buttons so that no matter what the background color of the button, it applies a filter that allows me to see a onPressed effect when the button is pressed.

I'm not sure how to update it from the deprecated setBackgroundDrawable, and LayerDrawable

Phil
  • 4,029
  • 9
  • 62
  • 107

1 Answers1

0

Nevermind, I'm an idiot. I simply replaced the setBackgroundDrawable methods with setBackground. All works as expected now.

Phil
  • 4,029
  • 9
  • 62
  • 107
  • 1
    There is usually pretty good documentation, and if you view the source code will have comment blocks (and maybe a link) that point to new method (or the deprecated method just simply references the new method anyway) – Mark Jun 30 '16 at 18:56
  • @MarkKeen - absolutely correct. Overlooking the obvious here, thank you! – Phil Jun 30 '16 at 18:57