I need to know screen resolution in pixels in application that is created in Python with Kivy. The specific question concerns Android. Kivy itself unfortunately doesn't want to transfer information obtained from GL to user, so I have to use workarounds. Under Android it's pyjnius, according to pyjnius manual and DisplayMetrics manual I've tried this code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.utils import platform
from kivy.core.window import Window
from kivy.graphics import Canvas, Rectangle, Color
myheight=-100
mywidth=-100
myDPI=-100
gDPI=-100
Window.fullsceen = True
if platform == 'android':
from jnius import autoclass
DisplayMetrics = autoclass('android.util.DisplayMetrics')
metrics = DisplayMetrics()
gDPI=metrics.getDeviceDensity()
myheight = metrics.heightPixels
mywidth = metrics.widthPixels
myDPI = metrics.densityDpi
class MainApp(App):
def build(self):
b = Button(text="Height: "+ str(myheight) + "\nWidth: " + str(mywidth) + "\nDPI: " + str(myDPI) + "\ngetDPI: " + str(gDPI)
+ '\nWindow.size:' + str(Window.size), color = [0,0,0,1])
with b.canvas:
Color(0,1,1, .2)
Rectangle(size = Window.size, pos = (0,0))
return b
MainApp().run()
But in the result I have all three values = 0, and window size = 800px independently from platfrom. Pyjnius manual use method getDeviceDensity() that is not documented in DisplayMetrics methods. Are there any alternative methods how can I detect screen resolution in kivy under android? Can I call directly SDL2 to detect screen resolution and where it has to be done properly?
The screenshot (as requested) is attached.
Thanks in advance.
This worked for me: