You can create a Bitmap with a solid color like this:
Bitmap bitmap = Bitmap.createBitmap(1, 1, Config.ARGB_8888);
bitmap.setPixel(0, 0, Color.BLUE); // Set color to blue
Bitmap resultBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
bitmap.recycle();
You can than wrap it with BitmapDrawable
if you really need a Drawable
but I can't really tell from your question what you need it for.
BitmapDrawable drawable = new BitmapDrawable(resultBitmap);
EDIT:
You can create a Drawable
with a border around it just like @vipulmittal suggested by using a GradientDrawable
.
GradientDrawable drawable = new GradientDrawable();
drawable.setStroke(width, Color.RED);
EDIT2:
The setBackground
method is for API level 16 and up and setBackgroundDrawable
is used for anything below API level 16. So what you have to do is something like this:
// Build.VERSION_CODES.JELLY_BEAN is API LEVEL 16
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
imageView.setBackground(drawable);
} else {
imageView.setBackgroundDrawable(drawable);
}
I tend to create a helper method for cases like this:
public static void setBackground(ImageView imageView, Drawable drawable) {
// Build.VERSION_CODES.JELLY_BEAN is API LEVEL 16
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
imageView.setBackground(drawable);
} else {
imageView.setBackgroundDrawable(drawable);
}
}
And then everytime you set the background of a ImageView
you just have to call:
ImageViewHelper.setBackground(imageView, drawable);