RecycleView miscomputing heights? or am I?

29 views
Skip to first unread message

John Perry

unread,
Sep 23, 2025, 12:19:32 PM (5 days ago) Sep 23
to Kivy users support
Hello

I have some structured information that I need to place inside a RecycleView. to do this, I am trying to add several equally-sized vertical BoxLayout's to the RecycleView, but it mis-computes their heights and wrecks the layout.

An easy way to replicate this is to redefine RVTextInput in the kivy example key_viewclass.py as follows:

<RVTextInput@BoxLayout>:
    value: ''
    BoxLayout:
        orientation: "vertical"
        BoxLayout:
            Label:
                text: root.title
                size_hint_y: None
                height: dp(60)
                font_size: dp(60)
            TextInput:
                text: root.value
                on_text: app.handle_update(self.text, root.index)
                size_hint_y: None
                height: dp(60)
                multiline: False
                font_size: dp(60)
        BoxLayout:
            Label:
                text: "test"
                size_hint_y: None
                height: dp(30)
                font_size: dp(30)
        BoxLayout:
            Label:
                text: "test"
                size_hint_y: None
                height: dp(30)
                font_size: dp(30)
        BoxLayout:
            Label:
                text: "test"
                size_hint_y: None
                height: dp(30)
                font_size: dp(30)


When running it with this change, the RVTextinputs overwrite things above and below them, including each other.
kivy_recycleview_bug3.jpg

As a "bonus", scrolling is also wrecked:
kivy_recycleview_bug4.jpg

While digging into this, I encountered evidence that something is overwriting, or possibly clamping, these elements' heights to be 100, and that throws off the computation of the sizes that kivy needs to compute their positions. I was not able to confirm this any further than comparing the elements' heights before and after adding each new element.

However, I may be setting this up wrong. I've tried a number of modifications of widget and layout heights, to no avail.

Can anyone suggest a way to accomplish what I'm trying to do with a RecycleView?  -- or perhaps with a different layout? the key is that there may be many elements, so I will need to scroll through them.

Thanks in advance

ElliotG

unread,
Sep 23, 2025, 12:26:18 PM (5 days ago) Sep 23
to Kivy users support
Your BoxLayouts all have a default size_hint of (1, 1).  You want the viewclass to have a fixed vertical size - but you have not given the layout a fixed vertical size.  

A BoxLayout is capable of setting the minimum size (or this case height) required to hold it's child widgets.  You can use the minimum_height attribute.

So for each of the BoxLayouts add the following:

BoxLayout:
    size_hint_y: None
    height: self.minimum_height

Let me know if that solves this for you.  If you continue to have trouble share a minimal runnable example, I'll be happy to help.

John Perry

unread,
Sep 24, 2025, 6:29:19 PM (4 days ago) Sep 24
to Kivy users support
Thank you; unfortunately, that does not seem to do the trick.

The attached archive should have a minimal working example. When it runs, pressing "Add" three times should give you something like what you see below. Note that
  • item "1" starts off in full view
  • item "2" shoves item "1" partially off screen, and also overlaps item "1"
  • item "3" shoves both "1" and "2" up
  • the box_description.kv makes use of your suggestion everywhere it possibly can
 kivy_recycleview_bug5.jpg
arghackle.zip

ElliotG

unread,
Sep 24, 2025, 9:05:48 PM (4 days ago) Sep 24
to Kivy users support
In viewer.kv, set the default_height.

    RecycleView:
        id: known_labels_view
        data: root.class_dict
        key_viewclass: "widget"
        size_hint_x: 0.2
        RecycleBoxLayout:
            id: known_labels_layout
            orientation: "vertical"
            size_hint_y: None
            height: self.minimum_height
            default_size_hint: 1, None
            default_height: dp(240)  # added default height


In box_description.kv I made some changes.  The most important was not basing the size of the widget based the on data.  The text values will be changed by the RecycleView.  This will cause them to change dynamically.  I changed these to use static values.  If you want widgets to be variable sizes you would use key_size. You also had defined BoxDescription twice.


Updated files attached. I also based Root on BoxLayout... There did not seem like a reason for the FloatLayout.
box_description.kv
main.py
viewer.kv

ElliotG

unread,
Sep 24, 2025, 9:11:19 PM (4 days ago) Sep 24
to Kivy users support
Here is a simple example of a Recycleview - you might find this helpful.
rvhelp.py

John Perry

unread,
Sep 25, 2025, 12:38:25 PM (3 days ago) Sep 25
to Kivy users support
Thank you! That does seem to help.

If I may, I have a few questions about your changes.
  1. I thought the whole point of self.texture_size was that it would automagically determine the correct size. However, you've replaced it with explicit dp values whose calculation is unexplained, and these values have to be propagated up the chain: not only the BoxLayout containing the label, but also the RecycleBoxLayout containing the BoxLayout containing the former. I can imagine situations where the font size is not known in advance. Is there a consistent, reliable method for kivy to auto-detect a label's height, and therefore its container's height, depending on the font size?
  2. Or, in general, for a container to auto-detect its contents' sizes, and determine its size based on that?
  3. You used what looks like a 20% padding around the font. That seems to work for a couple of other font sizes I've tried. Is that a safe bet in general?
  4. You mention that I had defined the BoxDescription twice. That was because I had been imitating the kivy example key_viewclass.py, actually. After reviewing it, do I infer correctly that the example does it that way in order to define the common parts of several classes?
Thank you again for any help you can give.

elli...@cox.net

unread,
Sep 25, 2025, 1:41:11 PM (3 days ago) Sep 25
to kivy-...@googlegroups.com
 "thought the whole point of self.texture_size was that it would automagically determine the correct size. However, you've replaced it with explicit dp values whose calculation is unexplained, and these values have to be propagated up the chain: not only the BoxLayout containing the label, but also the RecycleBoxLayout containing the BoxLayout containing the former. I can imagine situations where the font size is not known in advance. Is there a consistent, reliable method for kivy to auto-detect a label's height, and therefore its container's height, depending on the font size?"

Yes you can use testure_size in this context.  This was my mistake as I was getting to the root cause of your issue.  My concern was that the text size was chaging dynamically causing problem with the size - this was not the case.  Setting the default_height in the RecycleBoxLayout is sufficnet to address the issue.

  1. Or, in general, for a container to auto-detect its contents' sizes, and determine its size based on that?
  2. You used what looks like a 20% padding around the font. That seems to work for a couple of other font sizes I've tried. Is that a safe bet in general?
In your kv file, I turned the Label into a Button and used on_release to print the texture_size, I used that value in the code, again this was not necessary.

  1. You mention that I had defined the BoxDescription twice. That was because I had been imitating the kivy example key_viewclass.py, actually. After reviewing it, do I infer correctly that the example does it that way in order to define the common parts of several classes?
Yes, your understanding is correct.




From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of John Perry <cantani...@gmail.com>
Sent: Thursday, September 25, 2025 9:38 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] Re: RecycleView miscomputing heights? or am I?
 
--
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/80b960ac-8150-4a13-9bfe-f99b992e9150n%40googlegroups.com.

elli...@cox.net

unread,
Sep 25, 2025, 1:44:01 PM (3 days ago) Sep 25
to kivy-...@googlegroups.com
Below I updated the kv file... this still works correctly.  I left the size hints (default) as 1,1 in the BoxDescription as the size is being set by the enclosing RecycleBoxLayout.

<BoxDescription@BoxLayout>:
   
value: ""
   
box_top: ""
   
box_left: ""
   
orientation: "vertical"
   
BoxLayout:
       
orientation: "vertical"
       
size_hint_y: None
       
height: self.minimum_height
       
Label:
           
text: root.value
           size_hint_x
: 1
           
size_hint_y: None
           
font_size:  dp(60)
           
height: self.texture_size[1]

       
BoxLayout:
           
orientation: "horizontal"
           
size_hint_y: None
           
height: label_0.texture_size[1]
           
Label:
               
id: label_0
               
text: "top"
               
size_hint_x: 0.5
               
font_size: dp(30)
           
Label:
               
text: root.box_top
               
size_hint_x: 0.5
               
font_size: dp(30)

       
BoxLayout:
           
orientation: "horizontal"
           
size_hint_y: None
           
height: label_1.texture_size[1]
           
Label:
               
id: label_1
               
text: "left"
               
size_hint_x: 0.5
               
font_size: dp(30)
           
Label:
               
text: root.box_left
               
size_hint_x: 0.5
               
font_size: dp(30)


From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> on behalf of John Perry <cantani...@gmail.com>
Sent: Thursday, September 25, 2025 9:38 AM
To: Kivy users support <kivy-...@googlegroups.com>
Subject: [kivy-users] Re: RecycleView miscomputing heights? or am I?
 

ElliotG

unread,
Sep 25, 2025, 1:49:30 PM (3 days ago) Sep 25
to Kivy users support
Here is an example using key_size to set the size of widgets based on the texture_size of the text in the Label.
rv_console.py
Reply all
Reply to author
Forward
0 new messages