Search code examples
androidoptimizationkivyscreenresolution

Python Kivy: How to optimize screen resolution for all devices?


Im just started to coding with kivy and working on it.But im wondering, how can i optimize screen resolution for apk apps. I mean ,im using Window.size = (423,750) and controlling after coding with this resolution. So trying to create all widgets with boxlayouts, size_hints. But when im checking my application on Genymotion or Android devices, my widgets dont look like i created on linux.

For an example ,I use that for texts: font_size: '18sp'.But it doesn't looks like as i wanted.It's size changes when i check on different genymotion android devices and sometimes boxlayout blocks its size and makes it unreadable.

Is there a any command for auto screen resolution on each devices or should i create these font_size just like that : font_size: str(root.width*.05)+'sp' (So should i change my all codes like these spacing: 30 to spacing: root.height*.007 ) ? Any help would be good for beginners like me.How should i optimize all widgets and texts ? Thanks for reading and answering..


Solution

  • First off, DON'T use Window.size to control the size of the window since you don't know what size your device will be in the end. Second, in kv, you can define your font size with the sp() or dp() helper functions, for example:

    Label:
      text: 'Hello'
      font_size: sp(18)
    

    To preview your app while developing, you can test with different devices by:

    1. Checking the respective device's resolution
    2. Checking the device's dpi

    So, for example to emulate a Samsung S9+, which has a resolution of 1440x2960 and a dpi of 529. you would run your app like this:

    python main.py --size=1440x2960 --dpi=529

    This will give you the results you want, if using this method, i find it much easier to work with the dp() helper function instead of the sp() as what you have now is the dpi so kivy can use that.

    Just a quick note, if your screen is smaller than the specified size, e.g on my laptop with a resolution of 1366x768, you simply need to scale the size down, e.g run it as:

    python main.py --size=360x740 --dpi=529

    Its the same resolution but scaled to about a quarter of the original size to make it fit on your screen.

    FYI, you can also use the dp() and sp() functions in your .py file, simply import them like this:

    from kivy.metrics import dp, sp