0

Hello I am setting padding to the ImageView and each device is showing me different padding, somewhere it is less or somewhere it is more.

I am doing this

int padding = Util.dpFromPx(6, this);
thumbnailImageView.setPadding(padding, padding, padding, padding);
thumbnailImageView.setBackgroundColor(Color.WHITE);

// convert the px to dp
public static int dpFromPx(float px, Context context) {
    return (int) (px / context.getResources().getDisplayMetrics().density);
}

I want to keep padding same to all device. Any idea to do that ?

Sony device

enter image description here

Samsung Dous

enter image description here

Mick
  • 1,179
  • 5
  • 14
  • 24
  • You should convert dip to pixels like that: http://stackoverflow.com/questions/8399184/convert-dip-to-px-in-android – fpanizza May 05 '14 at 19:24

1 Answers1

1

SetPadding receive the values in pixels.

So you need to start from dips and convert to pixels:

int paddingInPx = Util.dipsToPixels(6);
thumbnailImageView.setPadding(paddingInPx , paddingInPx , paddingInPx , paddingInPx );

public int dipsToPixels(int dips)
    {
        return (dips * getApplicationContext().getResources().getDisplayMetrics().density + 0.5f);
    }
fpanizza
  • 1,589
  • 1
  • 8
  • 15