2

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.

Ukjent
  • 823
  • 2
  • 9
  • 26
  • maybe you can adapt this code: http://stackoverflow.com/questions/6957032/android-padding-left-a-bitmap-with-white-color – TryTryAgain Apr 11 '12 at 19:47

1 Answers1

3

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.

Greg
  • 196
  • 1
  • 3