Question

How can i create a rectangle drawable with border only not filling the color from code not from XML... Is there any way to accomplish this using code? Really Appreciate your helps...

Thanks @Xaver and @Vipul for the Answer :D

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);
Was it helpful?

Solution

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);

OTHER TIPS

You can simply create a gradient drawable object and set stroke to it.

    GradientDrawable gd=new GradientDrawable();
    gd.setStroke(1, Color.RED);

Just set this as background of any view and it will draw border to the view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top