Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to position image in the same spot on all devices?

I have realized that I am not 100% sure what is the best way to position some UI element so that it appears on the same spot on all devices.

So far these are the options:

  1. Set dp padding for each device
  2. Make View above this image and change it's padding for each device
  3. Calculate screen size in Java code and from the code set its locate in generics way. For example, set that image is located 1/5 screen height from top and apply this formula depending on current device's screen height.
  4. Similar to point 2., but instead of using fixed dp, I play around with layout_weight of elements in question

What seems to be the best way?

Is there a better way which I did not mention here?

Thanks

like image 719
sandalone Avatar asked Oct 19 '12 13:10

sandalone


1 Answers

The two best options are as follows:

  1. In code - calculate screen size and work with percentage of the screen.
  2. Use layout_weight for your views and weight_sum for the parent view.

A small example for the code approach - Lets say you want a View to be exactly 25 percent of screen width:

 WindowManager manager = (WindowManager) _context.getSystemService(Activity.WINDOW_SERVICE);
 int screenWidth = manager.getDefaultDisplay().getWidth();
 YOUR_VIEW.getLayoutParams().width = (int) (screenWidth * 0.25);
like image 93
Givi Avatar answered Nov 12 '22 13:11

Givi