Edwin
Edwin

Reputation: 805

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!!

Upvotes: 3

Views: 867

Answers (1)

John Anderson
John Anderson

Reputation: 38937

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]

Upvotes: 4

Related Questions