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

android, android-layout, dpi

Solution

The two best options are as follows:

- In code - calculate screen size and work with percentage of the screen.

- 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);

Problem

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: - Set `dp` padding for each device - Make `View` above this image and change it's padding for each device - 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. - 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

Original source