Forum Visual Studio
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Trying to Set Screen Resolution to Match Physical Device

kdt_dynkdt_dyn Member ✭✭

So the physical device we are targeting is 800h x 480w. When I run the app on that device Xamarin.Essentials.DeviceDisplay.MainDisplayInfo reports height=800, Width=480, Density=1.5 (What does that even mean, anyway? Density? Density of what? Pixels? And how does it come to 1.5? How can you have 1.5 pixels?)

However I want to be able to run on the emulator with the exact same "display", so that all the fonts, and alignments etc. look right. So I create an emulator image with hw.lcd.height=800, hw.lcd.width=480 , and hw.lcd.density=240 (which for some perplexing reason causes the density reported by xamarin.essentials to be 1.5)

The problem is that now I have this giant high-res device that is reported (by xamarin.essentials) to be 1920 high by 1080 wide! Why does it do this, when I have specifically stated to use 800h and 480w?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai

    According to the documentation of Android:
    https://developer.android.com/training/multiscreen/screendensities
    We use density-independent pixels (dp) as the unit of measurement due to different resolution screens.
    And this is the expression between pixel and dp:

     px = dp * (dpi / 160)
    

    The density you got from the MainDisplayInfo is the result of dpi / 160. This means if you want to get the real device pixel, replace dpi / 160 with density here.

  • kdt_dynkdt_dyn Member ✭✭

    @LandLu said:
    According to the documentation of Android:
    https://developer.android.com/training/multiscreen/screendensities
    We use density-independent pixels (dp) as the unit of measurement due to different resolution screens.
    And this is the expression between pixel and dp:

     px = dp * (dpi / 160)
    

    The density you got from the MainDisplayInfo is the result of dpi / 160. This means if you want to get the real device pixel, replace dpi / 160 with density here.

    OK, the target device reports 480x800 with density of 1.5 that should mean that the actual number of pixels is 480 x 1.5 by 800 x 1.5 = 720x1200. But it still keeps reporting 1080x1940! What the heck???

    It's this screen here:

  • LandLuLandLu Member, Xamarin Team Xamurai

    720x1200 is what displays on the image.
    What do you mean by But it still keeps reporting 1080x1940?
    Where do you get this information?

Sign In or Register to comment.