Is there a way to get the device pixel density?

I’m new to Corona. I’m trying to create an app that handles different screen sizes the way I want it to. Basically, what this means is that I want to manually calculate and position elements so I don’t have to stretch them or letterbox my app in different screen sizes.

I know the documentation recommends making one app for 320x480, but I’d rather just figure this all out in my program (at this point).

My issue is that I can easily get the display resolution. However, I don’t know how to get the pixel density of a given display. So, for example, the iphone 4 has 2x the pixel density of the iPhone 3. Is there a way to find this?

I specifically need this because of an apparent bug in display.statusBarHeight. The value is the same no mater if I’m using a retina display or not. As such, I can’t reliably place elements directly at the bottom of the status bar. If I knew that I was on a display with a 2x density I could scale this item’s position accordingly.

Any tips?

Thanks,

Doug [import]uid: 152958 topic_id: 27037 reply_id: 327037[/import]

Hi Doug, this will give you the actual width and height in pixels;

[lua]realWidth = (display.contentWidth - (display.screenOriginX * 2)) / display.contentScaleX
realHeight = (display.contentHeight - (display.screenOriginY * 2)) / display.contentScaleY

print (realWidth, realHeight)[/lua]

Peach :slight_smile: [import]uid: 52491 topic_id: 27037 reply_id: 109878[/import]

Is there a way to figure out pixel density - for android devices? [import]uid: 48484 topic_id: 27037 reply_id: 124003[/import]

I don’t think Corona provides an API for DPI.

For iOS devices it’s easily calculated hard coded as there are not many options (iPhone, iPhone4, iPhone5, iPad, iPad3)

But for Android the options are broader… Android SDK does expose the xdpi and ydpi, I wish Corona would have provided these too…

We also wanted this as it’s important when building a universal app. In some cases it’s important to know the physical size of the device. For example when using a small text on big devices would seem ok while on small devices we would prefer to enlarge the font to make it readable. [import]uid: 80469 topic_id: 27037 reply_id: 124026[/import]

Android guidlines also uses dpi and not pixels. [import]uid: 138389 topic_id: 27037 reply_id: 124028[/import]

How do we set font size for an input field that works for different android devices? Any best practices or workarounds available?

FOLLOWING ARE SOME OF THE (KNOWN) ISSUES - Any workarounds/suggestions?

– HEIGHT & POSITIONING of native text fields seem to be inconsistent on different ANDROID devices - for a CUSTOM FONT
nativeInputField = native.newTextField(100, 100, 144, 40)

– HOW DO WE SET FONT SIZE THAT WORKS FOR DIFFERENT ANDROID DEVICES?
local nativeFont = native.newFont( “Doctor Soos Light”, 18)
nativeInputField.font = nativeFont

– TEXT COLOR DOESN’T WORK ON ANDROID DEVICES
nativeInputField:setTextColor(255, 255, 255, 255)

– THIS DOESN’T WORK ON SIMULATOR
nativeInputField.hasBackground = false

nativeInputField.isEditable = true
[import]uid: 48484 topic_id: 27037 reply_id: 124074[/import]

How do we set font size for an input field that works for different android devices? Any best practices or workarounds available?  

I was testing on 480 x 800 Nexus One.
If you use a fontSize = 23 px on 480x800 screen then you can get that ratio on all screens.
[lua]local fontSize = 23 * (display.contentHeight / 800)[/lua]
Example: on Galaxy S3 720 x 1280 you will have display.contentHeight = 1280
fontSize = 23 * (1280 / 800) = 23 * 1.6 = 36,8 = 36

TEXT COLOR DOESN'T WORK ON ANDROID DEVICES  

It works for me.
[import]uid: 138389 topic_id: 27037 reply_id: 124077[/import]

Thanks for your suggestions, appreciate it very much.

I should have added the following details as well …

  1. Its a Corona Dynamic resolution app with my config file

content =
{
width = 320,
height = 480,
scale = “letterbox”
}

  1. My Android test devices - Moto Droid running Android 2.2.3 and Samsung Galaxy Tab 10.1 running Android 3.1. Text Color for native.TextField is always BLACK on these devices.

I tried your calculations for Font Size scale - not happy with the results, it still appears small for the tablet.

Also, could you please give the reasons behind your formula? Just for my understanding. [import]uid: 48484 topic_id: 27037 reply_id: 124087[/import]

Maybe Dynamic resolution is the problem, disable it and try again that formula.
That formula is very simple. [import]uid: 138389 topic_id: 27037 reply_id: 124091[/import]

Ok, I got your calculation - since you based your font on 800 pixel height device.

I tried without dynamic resolution and font color issue is still present.

So, may be Android OS version issue since it works on your devices?

I have been using corona dynamic resolution through out my app - and, I don’t seem to find a good workaround for font size (custom font) on different Android devices.

I believe, with Pixel density information for Android devices - we could come up with font sizes for different devices. [import]uid: 48484 topic_id: 27037 reply_id: 124130[/import]

Is there a way to figure out pixel density - for android devices? [import]uid: 48484 topic_id: 27037 reply_id: 124003[/import]

I don’t think Corona provides an API for DPI.

For iOS devices it’s easily calculated hard coded as there are not many options (iPhone, iPhone4, iPhone5, iPad, iPad3)

But for Android the options are broader… Android SDK does expose the xdpi and ydpi, I wish Corona would have provided these too…

We also wanted this as it’s important when building a universal app. In some cases it’s important to know the physical size of the device. For example when using a small text on big devices would seem ok while on small devices we would prefer to enlarge the font to make it readable. [import]uid: 80469 topic_id: 27037 reply_id: 124026[/import]

Android guidlines also uses dpi and not pixels. [import]uid: 138389 topic_id: 27037 reply_id: 124028[/import]

How do we set font size for an input field that works for different android devices? Any best practices or workarounds available?

FOLLOWING ARE SOME OF THE (KNOWN) ISSUES - Any workarounds/suggestions?

– HEIGHT & POSITIONING of native text fields seem to be inconsistent on different ANDROID devices - for a CUSTOM FONT
nativeInputField = native.newTextField(100, 100, 144, 40)

– HOW DO WE SET FONT SIZE THAT WORKS FOR DIFFERENT ANDROID DEVICES?
local nativeFont = native.newFont( “Doctor Soos Light”, 18)
nativeInputField.font = nativeFont

– TEXT COLOR DOESN’T WORK ON ANDROID DEVICES
nativeInputField:setTextColor(255, 255, 255, 255)

– THIS DOESN’T WORK ON SIMULATOR
nativeInputField.hasBackground = false

nativeInputField.isEditable = true
[import]uid: 48484 topic_id: 27037 reply_id: 124074[/import]

How do we set font size for an input field that works for different android devices? Any best practices or workarounds available?  

I was testing on 480 x 800 Nexus One.
If you use a fontSize = 23 px on 480x800 screen then you can get that ratio on all screens.
[lua]local fontSize = 23 * (display.contentHeight / 800)[/lua]
Example: on Galaxy S3 720 x 1280 you will have display.contentHeight = 1280
fontSize = 23 * (1280 / 800) = 23 * 1.6 = 36,8 = 36

TEXT COLOR DOESN'T WORK ON ANDROID DEVICES  

It works for me.
[import]uid: 138389 topic_id: 27037 reply_id: 124077[/import]

Thanks for your suggestions, appreciate it very much.

I should have added the following details as well …

  1. Its a Corona Dynamic resolution app with my config file

content =
{
width = 320,
height = 480,
scale = “letterbox”
}

  1. My Android test devices - Moto Droid running Android 2.2.3 and Samsung Galaxy Tab 10.1 running Android 3.1. Text Color for native.TextField is always BLACK on these devices.

I tried your calculations for Font Size scale - not happy with the results, it still appears small for the tablet.

Also, could you please give the reasons behind your formula? Just for my understanding. [import]uid: 48484 topic_id: 27037 reply_id: 124087[/import]

Maybe Dynamic resolution is the problem, disable it and try again that formula.
That formula is very simple. [import]uid: 138389 topic_id: 27037 reply_id: 124091[/import]

Ok, I got your calculation - since you based your font on 800 pixel height device.

I tried without dynamic resolution and font color issue is still present.

So, may be Android OS version issue since it works on your devices?

I have been using corona dynamic resolution through out my app - and, I don’t seem to find a good workaround for font size (custom font) on different Android devices.

I believe, with Pixel density information for Android devices - we could come up with font sizes for different devices. [import]uid: 48484 topic_id: 27037 reply_id: 124130[/import]

Just an update. Text Color for Android has been fixed on Build # 2012.919 [import]uid: 48484 topic_id: 27037 reply_id: 124805[/import]

Just an update. Text Color for Android has been fixed on Build # 2012.919 [import]uid: 48484 topic_id: 27037 reply_id: 124805[/import]