Kivy (kivymd) button font size is too large on Android, but works fine on pc

352 views
Skip to first unread message

The Golden

unread,
Nov 1, 2020, 4:16:26 AM11/1/20
to Kivy users support
In my kivymd app, I am using “sp” for font size. When I run the code on pc, it works fine. All widgets are working as they should. But on Android, the font size of widgets is too large though I am using sp. the text can’t even be seen fully.
MDRectangleFlatButton:
   font_size
: sp(0.5*self.height)

(I do have other properties such as text, but that was the thing that controls font size. 
I don’t think this is a problem or kivy metrics, since when I use “35sp” for my “Label” widget, it works as it should.

What I have already tried:
1. Using “dp” instead of “sp”; same result
2. Using it without “sp” or “dp” such as font_size: (0.5 * self.height); the text on button becomes smaller, but still larger than the button.
3. Using a specific value like “35sp” or “27sp”; same result (doesn’t change much)

Some points which might be useful:
1. I added print(dp(1)) and print(sp(1)) on the “on_start” method on app. It prints 1.0 for both the commands on pc. But on Android (I checked it using logcat using android studio), it prints 2.625 (I may be a few decimal over or under, don’t currently remember clearly). I don’t really know if it should be like that. Maybe due to screen resolution...

2. I have a slider which controls label size of a specific label on the app (it’s a HangMan game). I added a code with the label:
on_text:
   
print(self.font_size)

  It prints 65.0 (might not be exact) on Android. On pc I have set the label size as “font_size : sp(slider.value)”. The default slider value is 25. It prints 25 on PC though it prints 65.0 on phone. But the label size spins just what I want on Android. It looks the same as it does on pc. So surely metrics is working. 



How it looks on pc:

E0260427-1D95-431A-8942-EE27FA8148C7.jpeg

How it looks on phone:

EB799D00-5BB4-4E9E-8FCE-DF3E60E36A7E.jpeg






   

Elliot Garbus

unread,
Nov 1, 2020, 11:06:27 AM11/1/20
to kivy-...@googlegroups.com

The labels look fine  (“Enter Word”, “Category”).   How are they being specified?

 

You are doing a multiplying calculating ½ the height, this is going to scale with the widget size, so you don’t need the metrics.

Note there is a difference between the font_size and the texture_size. 

 

If you want to set the font_size by the height try converting the pixel height to point size.

font_size: self.height/pt(1)

 

Here is a small experiment.

 

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button

kv =
"""
BoxLayout:
    orientation: 'vertical'
    Button:
        size_hint_y: .7
        text: 'size_hint_y: .7'
        font_size: self.height/pt(1)
    Button:
        size_hint_y: 2
        text: 'size_hint_y: 2'
        font_size: self.height/pt(1)
    Button:
        text: 'Yet another string'
        font_size: self.height/pt(1)
    Button:
        text: 'set height by font_size'
        size_hint_y: None
        font_size: sp(50)
        height: pt(self.font_size)
    Button:
        text: 'set font_size by height'
        font_size: self.height/pt(1)
       
"""


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


ScaleFontApp().run()

How it looks on phone:

 

 

 

 

 

   

--
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 on the web visit https://groups.google.com/d/msgid/kivy-users/8aab19cb-09d0-447f-b91f-529ab9149683o%40googlegroups.com.

 

The Golden

unread,
Nov 1, 2020, 11:27:04 AM11/1/20
to Kivy users support
Oh “Enter word” and “category” are a part, of “MDTextField”, so it works, just as I said, font size of all other widgets work. 
I’ll try your suggestion


The Golden

unread,
Nov 1, 2020, 11:29:17 AM11/1/20
to Kivy users support

Another question, why does it work on pc then?
On Sunday, November 1, 2020 at 10:06:27 PM UTC+6 ElliotG wrote:

Elliot Garbus

unread,
Nov 1, 2020, 11:41:44 AM11/1/20
to kivy-...@googlegroups.com

There might be an issue with KivyMD, I have not used kivyMD.

 

On a Windows PC the density is 1 so there is generally no adjustment when using dp() or sp().  On your phone the density is over 2.6.

height is expressed in pixels. I’d expect the height value to be 2.6x what it is on the pc.

Robert Flatt

unread,
Nov 1, 2020, 12:26:27 PM11/1/20
to Kivy users support
The answer here https://github.com/kivymd/KivyMD/issues/307 perhaps gives a clue.

My limited experience is it is best to stick to the theme fonts, and don't try to out think KivyMD. 

KivyMD is a different implementation to Kivy, For detailed implementation of KivyMD widgets your
best resource is the source code or the GitHub issues forum (for best results have a developer level issue)

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

The Golden

unread,
Nov 1, 2020, 10:57:54 PM11/1/20
to Kivy users support
ElliotG: 
Your solution actually worked, i used
font_size: 0.6 * self.height/pt(1)

using pt worked!!

On Sunday, November 1, 2020 at 10:06:27 PM UTC+6 ElliotG wrote:

Elliot Garbus

unread,
Nov 1, 2020, 11:03:48 PM11/1/20
to kivy-...@googlegroups.com
👍

Sent from my iPad

On Nov 1, 2020, at 8:58 PM, The Golden <raiyana...@gmail.com> wrote:

ElliotG: 
Reply all
Reply to author
Forward
0 new messages