-2

I have a picture of fixed height (60 px by 40 px).

I am trying to position the image by programmatically by adding margins to the image so that the image aligns to the center of the view

How I can get it done correctly to work for all different size and devices?

borax12
  • 423
  • 1
  • 5
  • 16
  • Your question would benefit from a diagram of what you're trying to achieve, more detail about the environment (Are you making an app? What with?) and an example of what you've tried in the context. – user52889 Jul 15 '15 at 20:31

2 Answers2

0

Make the image have a width and height of wrap content and a layout_gravity of centerHorizontal

borax12
  • 423
  • 1
  • 5
  • 16
0

You can make image params to match parent and then add margins of your choice.

       ImageView image = new ImageView(this); 
       params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
       params.setMargins(left, top, right, bottom);
       image.setLayoutParams(params);
Karolis Jc
  • 106
  • 3