Reputation: 823
The Question is: I have an Imageview and I want to create a white border/padding with 2 dp width around the imageview. The imageview is decleared in java, and I want to do the padding in java, not xml.
Upvotes: 2
Views: 4669
Reputation: 196
ImageView supports 2 things : a background, and a Bitmap in the foreground. Both can be set to Drawables, Bitmaps or Resources from your XML.
So in Java, you should be able to do this :
ImageView view = new ImageView(this);
view.setImageResource(R.drawable.splash); // Adds the foreground Bitmap
view.setScaleType(ScaleType.CENTER_INSIDE); // Sets how the bitmap is scaled in it's container
view.setBackgroundColor(Color.WHITE); // Define the border color
view.setPadding(2,2,2,2); // Define the border size
view.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
This should do the trick.
Upvotes: 3