views:

27

answers:

1

Is it possible to create a ColorDrawable object without using xml? I would like to be able to change the backgroundColor of a view programmatically, using setBackgroundColor() or setBackgroundDrawable() or setBackgroundResource(), but I want to be able to specify the RGB values in code, not XML. Is this possible?

A: 

I know you can get a View as a Drawable and apply a color filter to it (useful for coloring in Button views) by doing the following:

Drawable d_delete = findViewById(R.id.btn_delete).getBackground();
PorterDuffColorFilter filter_red = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
d_delete.setColorFilter(filter_red);
Bryan Denny