Edwin
Edwin

Reputation: 805

How to add more than one layout in a single screen in kivymd

I am trying to add a boxlayout to a screen with a boxlayout already, but the contents of the second boxlayout keeps overlaying the contents of the first layout. I don't think it's my indentation, is there a code I am missing, or anything.

I want the content of the second boxlayout to come last of course. I will really appreciate any help, Thanks in Advance.

here's my code:

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

Window.size = (300, 530)

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

                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    height: label1.height
                    # size: 280, 200
                    MDLabel:
                        id: label1
                        markup: True
                        padding: [15, 1]
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            [b][i]“Make every detail perfect and limit the number of details to perfect.[/b][/i]”
                            – Jack Dorsey\n
                            '''

                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label2.height
                    MDLabel:
                        id: label2
                        markup: True
                        padding: [15, 5]
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            [b][i]“Make every detail perfect and limit the number of details to perfect.[/b][/i]”
                            – Jack Dorsey\n
                            '''

                MDCard:
                    orientation: 'vertical'
                    size_hint: 1, None
                    # size: 280, 200
                    height: label2.height
                    MDLabel:
                        id: label2
                        markup: True
                        padding: [15, 5]
                        size_hint_y: None
                        height: self.texture_size[1] + 2*self.padding[1]
                        text:
                            '''
                            [size=25][b]Ford[/b][/size]
                            [b][i]“Make every detail perfect and limit the number of details to perfect.[/b][/i]”
                            – Jack Dorsey\n
                            '''

    MDBoxLayout:
        orientation: 'vertical'
        ScrollView:
        MDGridLayout:
            cols: 9
            spacing: '10dp'
            padding: ['10dp', '10dp']

            MDCard:
                ripple_behavior: True
                orientation: 'vertical'
                size_hint: None, None
                size: "250dp", "180dp"
                elevation: 15
                radius: 15
                caption: 'Hello dear'
                Image:
                    allow_stretch: True
                    keep_ratio: False
                    size_hint_y: 1
                    source: "C:/Users/HP USER/Downloads/bella_baron.jpg"
            MDCard:
                ripple_behavior: True
                orientation: 'vertical'
                size_hint: None, None
                size: "250dp", "180dp"
                elevation: 15
                radius: 15
                caption: 'Hello dear'
                Image:
                    allow_stretch: True
                    keep_ratio: False
                    size_hint_y: 1
                    source: "C:/Users/HP USER/Downloads/bella_baron.jpg"
            MDCard:
                ripple_behavior: True
                orientation: 'vertical'
                size_hint: None, None
                size: "250dp", "180dp"
                elevation: 15
                radius: 15
                caption: 'Hello dear'
                Image:
                    allow_stretch: True
                    keep_ratio: False
                    size_hint_y: 1
                    source: "C:/Users/HP USER/Downloads/bella_baron.jpg"

"""


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


Example().run()

Upvotes: 0

Views: 1072

Answers (2)

Nux ツ
Nux ツ

Reputation: 156

From Kivy docs:

The class RelativeLayout behaves just like the regular FloatLayout except that its child widgets are positioned relative to the layout.

You have to put everything inside a BoxLayout to override the default RelativeLayout behaviour of MDScreen:

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

Window.size = (300, 530)

KV = """
<MyImageCard@MDCard>
    source: ''
    caption:''    
    ripple_behavior: True
    orientation: 'vertical'
    size_hint: None, None
    size: "250dp", "180dp"
    elevation: 15
    radius: 15       
    padding: "8dp"
    MDLabel:
        text: root.caption
        theme_text_color: "Secondary"
        adaptive_height: True
    MDSeparator:
        height: "1dp"
    Image:
        allow_stretch: True
        keep_ratio: False
        size_hint_y: 1
        source: root.source
        
<MyTextCard@MDCard>:
    text:""
    orientation: 'vertical'
    size_hint: 1, None
    height: child_label.height
    MDLabel:
        id: child_label
        markup: True
        padding: [15, 1]
        size_hint_y: None
        height: self.texture_size[1] + 2*self.padding[1]
        text:root.text
    
MDScreen:
    image: "C:/Users/HP USER/Downloads/bella_baron.jpg"           
    text:'[size=25][b]Ford[/b][/size][b][i]\\n"Make every detail perfect and limit the number of details to perfect."[/b][/i] \\n– Jack Dorsey'
    MDBoxLayout:
        orientation: 'vertical'
        size_hint: 1, 1
        pos_hint:{"center_x":.5,"center_y":.5}
        ScrollView:
            MDGridLayout:
                cols: 1
                adaptive_height: True
                padding: '10dp', '15dp'
                spacing: '15dp'
                
                MyTextCard:
                    text:root.text
    
                MyTextCard:
                    text:root.text
                                
                MyTextCard:
                    text:root.text
                
                MyTextCard:
                    text:root.text
                
                MyTextCard:
                    text:root.text
                        
                MDBoxLayout:
                    orientation: 'vertical'
                    size_hint: 1, None
                    height: 400                    
                    ScrollView:
                        MDGridLayout:
                            cols: 3                                 
                            adaptive_height: True               
                            adaptive_width: True        
                            spacing: '10dp'
                            padding: ['10dp', '10dp']
                            MyImageCard:
                                source: root.image
                                caption: 'Hello dear'                        
                            MyImageCard:
                                source: root.image
                                caption: 'Lovely'
                            MyImageCard:
                                source: root.image
                                caption: 'See you'
                            MyImageCard:
                                source: root.image
                                caption: 'Later'                        
                            MyImageCard:
                                source: root.image
                                caption: 'Forever'
                            MyImageCard:
                                source: root.image
                                caption: 'Good Bye'

"""


class Example(MDApp):

    def build(self):
        return Builder.load_string(KV)


Example().run()

Also you were missing an indentation level after the second Scrollview, but that was not the source of the problem :)

Upvotes: 1

John Anderson
John Anderson

Reputation: 38947

You can use pos_hint and size_hint to get what I think you want. Start your kv with:

MDScreen:
    MDBoxLayout:
        size_hint: 1, 0.5  # use half the available height of the MDScreen
        pos_hint: {'top':1}  # position at the top of the MDSCreen

and for the second MDBoxLayout add similar code:

    MDBoxLayout:
        size_hint: 1, 0.5  # use half the available height of the MDScreen
        pos_hint: {'y':0}  # position at the bottom of the MDSCreen

Upvotes: 1

Related Questions