Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Created February 24, 2015 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakTaccardi/2b6b4ddd36ccd0b6b000 to your computer and use it in GitHub Desktop.
Save ZakTaccardi/2b6b4ddd36ccd0b6b000 to your computer and use it in GitHub Desktop.
Setting Background Drawable with Material Button
public BaseButton(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.MaterialButton,
0, 0);
try {
colorButtonNormal = a.getInteger(R.styleable.MaterialButton_mbColorButtonNormal, Color.CYAN);
} finally {
a.recycle();
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
RippleDrawable draw = (RippleDrawable) getContext().getApplicationContext().getResources().getDrawable(R.drawable.raised_btn);
InsetDrawable inset = (InsetDrawable) draw.getDrawable(0);
GradientDrawable shape = (GradientDrawable) inset.getDrawable();
shape.setColor(colorButtonNormal);
setBackground(draw);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
InsetDrawable draw = (InsetDrawable) getContext().getApplicationContext().getResources().getDrawable(R.drawable.raised_btn);
GradientDrawable shape = (GradientDrawable) draw.getDrawable();
shape.setColor(colorButtonNormal);
setBackground(draw);
} else {
//todo
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment