I can’t calculate the correct safeArea because some Android devices (Xiaomi Mi 11 Lite, Xiaomi Readmi 9 Pro, etc.) return wrong values for display resolution (1080x2030px instead 1080x2400px).
Someone can help me because I can’t publish the app knowing that has problems with user interface (buttons outside safe area).
-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
-- Your code here
display.setStatusBar(display.HiddenStatusBar)
native.setProperty("androidSystemUiVisibility", "immersiveSticky")
local topInset, leftInset, bottomInset, rightInset = display.getSafeAreaInsets()
local orientation = system.orientation
local SCREEN = {
LEFT = display.screenOriginX,
TOP = display.screenOriginY,
RIGHT = display.screenOriginX + display.viewableContentWidth,
BOTTOM = display.screenOriginY + display.viewableContentHeight,
WIDTH = display.contentWidth,
HEIGHT = display.contentHeight,
DWIDTH = display.contentWidth - display.viewableContentWidth,
DHEIGHT = display.contentHeight - display.viewableContentHeight,
CENTER_X = display.contentCenterX,
CENTER_Y = display.contentCenterY,
SAFE_LEFT = display.screenOriginX + leftInset,
SAFE_TOP = display.screenOriginY + topInset,
SAFE_RIGHT = display.screenOriginX + display.viewableContentWidth - rightInset,
SAFE_BOTTOM = display.screenOriginY + display.viewableContentHeight - bottomInset,
SAFE_WIDTH = display.viewableContentWidth - (leftInset + rightInset),
SAFE_HEIGHT = display.viewableContentHeight - (topInset + bottomInset),
LEFT_INSET = leftInset,
RIGHT_INSET = rightInset,
TOP_INSET = topInset,
BOTTOM_INSET = bottomInset,
HAS_SOFTWARE_KEYS = system.getInfo("hasSoftwareKeys"),
MANUFACTURER = system.getInfo("manufacturer"),
MODEL = system.getInfo("model")
}
local displayRect = display.newRect(SCREEN.LEFT + 5, SCREEN.TOP + 5, display.viewableContentWidth - 10, display.viewableContentHeight - 10)
displayRect.anchorX, displayRect.anchorY = 0, 0
displayRect:setStrokeColor(1, 0, 0)
displayRect.strokeWidth = 6
local safeRect = display.newRect(SCREEN.SAFE_LEFT + 5, SCREEN.SAFE_TOP + 5, SCREEN.SAFE_WIDTH - 10, SCREEN.SAFE_HEIGHT - 10)
safeRect.anchorX, safeRect.anchorY = 0, 0
safeRect:setStrokeColor(0, 1, 0)
safeRect.strokeWidth = 3
local log = ""
log = log .. "\ncontentWidth : " .. string.format("%.1f", SCREEN.WIDTH) .. " , contentHeight : " .. string.format("%.1f", SCREEN.HEIGHT)
log = log .. "\nviewableContentWidth : " .. string.format("%.1f", display.viewableContentWidth) .. " , viewableContentHeight : " .. string.format("%.1f", display.viewableContentHeight)
if orientation == 'landscapeLeft' or orientation == 'landscapeRight' then
log = log .. "\npixelWidth : " .. tostring(display.pixelHeight) .. " , pixelxHeight : " .. tostring(display.pixelWidth)
else
log = log .. "\npixelWidth : " .. tostring(display.pixelWidth) .. " , pixelxHeight : " .. tostring(display.pixelHeight)
end
log = log .. "\n"
log = log .. "\nleft : " .. string.format("%.1f", SCREEN.LEFT) .. " , top : " .. string.format("%.1f", SCREEN.TOP)
log = log .. " , right : " .. string.format("%.1f", SCREEN.RIGHT) .. " , bottom : " .. string.format("%.1f", SCREEN.BOTTOM)
log = log .. "\ntopStatusBarContentHeight : " .. string.format("%.1f", display.topStatusBarContentHeight)
log = log .. "\n"
log = log .. "\nleftInset : " .. string.format("%.1f", SCREEN.LEFT_INSET) .. " , topInset : " .. string.format("%.1f", SCREEN.TOP_INSET)
log = log .. " , rightInset : " .. string.format("%.1f", SCREEN.RIGHT_INSET) .. " , bottomInset : " .. string.format("%.1f", SCREEN.BOTTOM_INSET)
log = log .. "\n"
log = log .. "\nsafeLeft : " .. string.format("%.1f", SCREEN.SAFE_LEFT) .. " , safeTop : " .. string.format("%.1f", SCREEN.SAFE_TOP)
log = log .. " , safeRight : " .. string.format("%.1f", SCREEN.SAFE_RIGHT) .. " , safeBottom : " .. string.format("%.1f", SCREEN.SAFE_BOTTOM)
log = log .. "\nsafeWidth : " .. string.format("%.1f", SCREEN.SAFE_WIDTH) .. " , safeHeight : " .. string.format("%.1f", SCREEN.SAFE_HEIGHT)
log = log .. "\n"
log = log .. "\nmanufaturer : " .. SCREEN.MANUFACTURER .. " , model : " .. SCREEN.MODEL .. " , hasSoftwareKeys : " .. tostring(SCREEN.HAS_SOFTWARE_KEYS)
log = log .. "\n"
log = log .. "\nRed rectangle (viewable area) , Green rectangle (safe area) "
local txtLog = display.newText(log, SCREEN.SAFE_LEFT + 10, SCREEN.SAFE_TOP + 30, native.systemFont, 20)
txtLog:setFillColor( 0, 0, 0 )
txtLog.anchorX = 0
txtLog.anchorY = 0
Screenshot from Xiaomi Mi 11 Lite device
Screensh from simulator using 1080x2400 for display resolution