프로그래밍/Android

GradientDrawable활용 button shape selector 코드 처리

프리월드 2017. 7. 20. 11:42
public static void setButtonBackground(Button btn, int normal_color, int press_color) {
StateListDrawable res = new StateListDrawable();
res.addState(new int[]{android.R.attr.state_pressed}, setGradientColors(normal_color));
res.addState(new int[]{}, setGradientColors(press_color));
btn.setBackground(res);
}

public static GradientDrawable setGradientColors(int color) {
GradientDrawable gradient = new GradientDrawable();
gradient.setShape(GradientDrawable.RECTANGLE);
gradient.setColor(color);
gradient.setCornerRadius(120.f);
return gradient;
}