Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App with Java Creating the Screen Layout Adding a TextView

Santiago Serrano
Santiago Serrano
2,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.

Please help...

1 Answer

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.