Search code examples
python-3.xkivykivymd

How do I automatically Set MDCard height


I want to set mdcard height such that it fits mdlabel, perhabs without me manually setting mdcard size. please how do I do this.. Thanks in Advance..

For better understanding here's a sample code:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.core.window import Window

Window.size = (300, 530)

KV = """
MDBoxLayout:
    orientation: 'vertical'
    ScrollView:
        MDGridLayout:
            cols: 1
            adaptive_height: True
            padding: '10dp', '15dp'
            spacing: '15dp'

            MDCard:
                orientation: 'vertical'
                size_hint: None, None
                size: 280, 200
                MDLabel:
                    markup: True
                    padding: [15, 15]
                    text:
                        '''
                        [size=25][b]Ford[/b][/size]
                        If mdlabel text becomes too many to fit into the specified mdcard size,\n
                        the text overslaps other things closeby..\n
                        So my question: How do I set MDCard height to automatically\n
                        adjusts accordingly to the height/size of widgets inside of it.\n
                        Thanks in Advance!!
                        '''

            MDCard:
                orientation: 'vertical'
                size_hint: None, None
                size: 280, 200
                MDLabel:
                    markup: True
                    padding: [15, 15]
                    text:
                        '''
                        [size=25][b]Ford[/b][/size]
                        If mdlabel text becomes too many to fit into the specified mdcard size,\n
                        the text overslaps other things closeby..\n
                        So my question: How do I set MDCard height to automatically\n
                        adjusts accordingly to the height/size of widgets inside of it.\n
                        Thanks in Advance!!
                        '''



"""


class Example(MDApp):
    def build(self):
        return Builder.load_string(KV)


Example().run()

just like the code above, If mdlabel text becomes too many to fit into the specified mdcard size, the text overslaps other things closeby.. So my question: How do I set MDCard height to automatically adjusts accordingly to the height/size of widgets inside of it. Thanks in Advance!!


Solution

  • You can set the height of the MDCard to be the same as the height of the MDLabel, but you must allow the MDLabel to adjust to the size of its text. Here is a modified version of your kv that does that:

    MDBoxLayout:
        orientation: 'vertical'
        ScrollView:
            MDGridLayout:
                cols: 1
                adaptive_height: True
                padding: '10dp', '15dp'
                spacing: '15dp'
    
                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label1.height
                    MDLabel:
                        id: label1
                        markup: True
                        padding: [15, 15]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            If mdlabel text becomes too many to fit into the specified mdcard size,\n
                            the text overslaps other things closeby..\n
                            So my question: How do I set MDCard height to automatically\n
                            adjusts accordingly to the height/size of widgets inside of it.\n
                            Thanks in Advance!!
                            '''
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
    
                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label2.height
                    MDLabel:
                        id: label2
                        markup: True
                        padding: [15, 15]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            If mdlabel text becomes too many to fit into the specified mdcard size,\n
                            the text overslaps other things closeby..\n
                            So my question: How do I set MDCard height to automatically\n
                            adjusts accordingly to the height/size of widgets inside of it.\n
                            Thanks in Advance!!
                            '''
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]