Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Santiago Serrano2,754 Points
When I set the size of a view in dp, does its physical size remain the same?
Ok, according to the Google Developers' guide "Support different pixel densities", one dp is a virtual pixel unit that's roughly equal to one pixel on a medium-density screen.
Correct me if I'm wrong, but if we do the math, since a medium-density screen is 160dpi, a dp should be equal to 1inch/160. Since this result would be in inches, a view whose size is set in dp (or in sp) should have the same physical size, no matter in which screen size and in which pixel density it is displayed.
But there is a thing that doesn't make sense to me (if what I stated before is correct). Let's say I have a button which will be 10 x 10cm in a TV. In a phone, that button would be ridiculously huge. This would mean that dp wouldn't be that good to use when using different screen sizes.
The problem here is screen density vs screen size. Phones come in different densities and, you are correct, to make sure that a view will have the same size on different phone screens, no matter the screen density, we use the dp unit (instead of pixels).
However, screen size is a different matter. If you want your Android phone app to look OK on a TV screen (or vice versa), you will have to rethink the UI for each of these screens. I am not familiar with developing for TVs or Wear OS, but when you build an Android app for phones, it is common to create a separate layout for tablets, and also sometimes a separate layout for phones in landscape mode. I'm afraid there is no one-size-fits-all layout for phones, tablets, TVs, Wear OS and Android Auto.