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:
dp
padding for each deviceView
above this image and change it's padding for each deviceJava 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. dp
, I play around with layout_weight
of elements in questionWhat seems to be the best way?
Is there a better way which I did not mention here?
Thanks
The two best options are as follows:
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With