Control over sizes during device rotation

31 views
Skip to first unread message

Radosław Głębicki

unread,
Apr 27, 2026, 10:14:55 PMApr 27
to Kivy users support
HI

Is this correct or can be done better:

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

from kivy.app import runTouchApp, stopTouchApp
from kivy.lang import Builder
from kivy.core.window import Window
#
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout as BL

tNN = (None,None)

WinW, WinH = Window.size
# always winS has short size of Window and WinL has long side
WinL, WinS = WinW, WinH
if WinW < WinH: # vertical
WinL, WinS = WinH, WinW

gnDefFontSize = int(.02 * WinL)     # font is 2% of long side
Builder.load_string(f"""
<Label>:
font_size: dp({gnDefFontSize})  # is `dp` really necessary ???????????
""")

def f_popup(*d):
popCont = BL(orientation = "horizontal", size_hint = (1,1))
popup   = Popup(title = 'Test',title_size = gnDefFontSize, background_color = (.51,.51,.5,1), auto_dismiss = True,
                           content = popCont, size_hint = (None,None), width = .9 * WinS, height = .9 * WinS)
clsBtn = Button(text = 'Close', size_hint = tNN, size = (.25 * WinS, .1 *WinS))
clsBtn.bind(on_release=popup.dismiss)
popCont.add_widget(Widget())
popCont.add_widget(clsBtn)
popCont.add_widget(Widget())
popup.open()
return None

mainLay = BL(orientation = 'horizontal', size_hint = (1,1))
btn = Button(text = "Popup", size_hint = tNN, size = (.25 * WinS, .1 *WinS))
btn.bind(on_release = f_popup)
btnX = Button(text = "X", size_hint = tNN, size = (.1 * WinS, .1 *WinS))
btnX.bind(on_release = lambda _: (stopTouchApp(), quit()))
mainLay.add_widget(Widget())
mainLay.add_widget(btn)
mainLay.add_widget(Widget())
mainLay.add_widget(btnX)

runTouchApp(mainLay)
quit()

save and for vertical size of window start:
script.py  --size 400x800
for horizontal, start:
script.py  --size 800x400

Radek Glebicki

elli...@cox.net

unread,
Apr 28, 2026, 9:55:50 PMApr 28
to kivy-...@googlegroups.com
I have some different preferences.
I would layout out the Popup and widgets in kv.

I prefer to use fixed sizes for buttons and fonts rather than use size hints.  

from kivy.app import App
from kivy.lang import Builder

kv =
"""
#:import Factory kivy.factory.Factory

<TestPopup@Popup>:
   size_hint: .9, .9
   title: 'Test Popup'
   BoxLayout:
       orientation: 'vertical'
       Label:
           text: 'Your content here'
       Button:
           size_hint: None, None
           size: dp(150), dp(48)
           text: 'Close'
           on_release: root.dismiss()
           pos_hint: {'center_x': .5}

AnchorLayout: # The root widget, put the button in the center
   Button:
       size_hint: None, None
       size: dp(150), dp(48)
       text: 'Popup'
       on_release: Factory.TestPopup().open()
   

"""

class PopApp(App):
   
def build(self):
       
return Builder.load_string(kv)

PopApp().run()





From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of Radosław Głębicki <glebicki...@gmail.com>
Sent: Monday, April 27, 2026 7:14 PM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] Control over sizes during device rotation
 
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/e8e11a24-af1e-4f59-9c9d-8d777a9fe633n%40googlegroups.com.

Radosław Głębicki

unread,
Apr 28, 2026, 10:50:55 PMApr 28
to Kivy users support
But with different size of the screen in device you get different final effect?
This not bother you?

Radek Glebicki

elli...@cox.net

unread,
Apr 29, 2026, 10:15:56 AMApr 29
to kivy-...@googlegroups.com
But with different size of the screen in device you get different final effect?

In general:
  • Do not scale the entire UI proportionally
  • Maintain minimum usable sizes
  • Use layout changes and spacing to adapt to larger screens
  • Keep typography within readable bounds
  • Layouts adapt, not shrink/stretch
This approach matches modern mobile and web UI practices and leads to more consistent usability across devices.  Apple and Google Material guidelines—both recommend keeping text and touch targets consistent and adapting layout and spacing rather than scaling the entire UI.
I'd suggest looking at Google's Material Design Guide,  The Apple Human Interface Guidelines,  and MDN Web Docs.

Sent: Tuesday, April 28, 2026 7:50 PM

To: Kivy users support <kivy-...@googlegroups.com>
Subject: Re: [kivy-users] Control over sizes during device rotation
 

Radosław Głębicki

unread,
Apr 29, 2026, 6:26:02 PMApr 29
to Kivy users support
Please explain me something about that "unit" `dp`.
I have phone fullhd 1920x1080 but 7 inches and tablet also fulhd 14 inches.
Using that same size 100x100 but with `dp` on tablet will by bigger like 100%?
I assume `dp` means `dpi` - dots per inch - dots here means pixels?

Radek Glebicki

elli...@cox.net

unread,
Apr 29, 2026, 10:38:17 PMApr 29
to kivy-...@googlegroups.com

Density-independent Pixels - An abstract unit that is based on the physical density of the screen. With a density of 1, 1dp is equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up a factor appropriate to the screen’s dpi, and the inverse for a lower dpi. The ratio of dp-to-pixels will change with the screen density, but not necessarily in direct proportion. Using the dp unit is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In others words, it provides consistency for the real-world size of your UI across different devices.

Sent: Wednesday, April 29, 2026 3:26 PM
Reply all
Reply to author
Forward
0 new messages