0

In my activity, I use DisplayMetrics to dynamically get the pixel height and width of the screen, and then I assign each of the components in the Activity sizes based on those dimensions. I wanted to know how this could be affected by screens that have different densities? Is it a good idea to use pixels?

Edit:

The purpose of using pixels dynamically is so that my layout scales based on the given screen. I just want to know how density will play into this. For example, if I have two screens with a height of 1024px and width of 800px, but one is twice as dense as the other, and I want to use 40% of the height and 40% of the width (this is just hypothetical) for a button, why should the density matter? This will just mean that the size of the button will have more pixels in the higher density screen, but the physical size of the button will be the same as DisplayMetrics will always give me the absolute size in pixels. Or am I wrong about this?

JD Sal
  • 5
  • 4
  • 1
    Pixel-based graphics can cause problems unless you're able to guarantee the size and resolution of the screen. A line 120 pixels long would be one inch on a 120dpi screen, but only half an inch long on a 240dpi screen and just a quarter of an inch long on a 480dpi screen. – David Jul 11 '17 at 19:39
  • Please see my edit – JD Sal Jul 12 '17 at 16:51

2 Answers2

1

See this question and its answers. You will get the answer to your question.

Edit: From one of the answers on the mentioned question

If you are any serious about developing an Android app for more than one type of device, you should have read the screens support development document at least once. In addition to that it is always a good thing to know the actual number of active devices that have a particular screen configuration.

Screen Sizes and Densities

Community
  • 1
  • 1
Birendra Singh
  • 842
  • 11
  • 19
  • 1
    Link only answers are discouraged. Either leave it as a comment, mark the question as a duplicate, or put the relevant information from the other answer here. – takendarkk Jul 11 '17 at 19:47
  • This does not answer my question. – JD Sal Jul 13 '17 at 16:51
  • I feel you are asking about what you should use when you are instantiating a view programmatically? If it the case then I want to tell you that the view methods take only pixel values. When you are saying that you use `DisplayMetrics`, you are doing the same the system does (with *dp* values). Also that is the correct way to render an item with same (almost) size on all screens. You define `w=15` and then use `w_px = w * density.` Then actually `w` is in `dp.` – Birendra Singh Jul 13 '17 at 17:17
0

To help in your case it is proposed to use dp units instead of pixels, but still there will be differences from one device to another.

On a tablet screen with a high pixel density, the elements probably will occupy less relative space.

If you want to improve it more then you will have to do the dimensions calculation by your own.

Or use a layout that auto distributes the space, for example the LinearLayout

Also you have to take into account that it is the system that decides the size of some widgets, for example the standard buttons

from56
  • 3,976
  • 2
  • 13
  • 23
  • So am I right to assume that if I'm using percentage to decide the size of elements in my layout, density shouldn't matter as DisplayMetrics will give me the absolute size in pixels? And the element will be same size across all screens of the same dimensions regardless of their densities? – JD Sal Jul 13 '17 at 15:39
  • But, not all the screens have the same width/height ratio, and the screen have portrait & landscape orientation. Do you plan to redim all elements or only some ? What with font sizes ? or border thickness if any ? .. it's not easy. – from56 Jul 13 '17 at 15:45
  • Yes I plan to redeem everything, for both land and portrait. The entire layout consists of buttons and text views, so all of them are meant to scale according to the dimensions of the screen. Even font sizes. That is why I am using percentages. – JD Sal Jul 13 '17 at 16:22
  • @JD Be cautious using percentage. What if your call to size change changes the dimention relative to which you calculated the size? Will not it fall in an infinite loop - a game neither you win nor the system? I mean you will not get what you want in a single call only. – Birendra Singh Jul 15 '17 at 22:02
  • What do you mean by "What if your call to size change changes the dimention relative to which you calculated the size"? – JD Sal Jul 16 '17 at 10:43