Kivymd MDSegmentedControl size

337 Views Asked by At

I'm trying to use the MDSegmentedControl but can't figure out how to adjust the overall width. It just runs off the screen when placed next to another widget.

KV = '''
MDScreen:
    
    
    
    MDCard:
        MDLabel:
            size_hint: None,1
            width: 200
            text: 'Gender'
        
        MDSegmentedControl:
            adaptive_width: True
            pos_hint: {"center_x": .5, "center_y": .5}
    
            MDSegmentedControlItem:
                text: "Male"
    
            MDSegmentedControlItem:
                text: "Female"
                
 
'''


class Example(MDApp):
    def build(self):
        self.theme_cls.theme_style = "Dark"
        self.theme_cls.primary_palette = "Orange"
        return Builder.load_string(KV)


Example().run()
2

There are 2 best solutions below

0
On

This will do the job:

sc = MDSegmentedControl(pos_hint={'center_x': 0.5, 'center_y': 0.5}, size_hint=(1,None))
sc.ids.segment_panel.pos_hint = (1,1)
0
On

For me, I had to give a fixed width of the segment panel children: https://github.com/kivymd/KivyMD/blob/master/kivymd/uix/segmentedcontrol/segmentedcontrol.kv#L34

Its width is set as 320dp, no matter what size you give to the MDSegmentedControl widget.

So once the widget is created, adjust the width of the segment_panel children by yourself:

sc = MDSegmentedControl(size_hint_x=None, width=dp(200))
sc.ids.segment_panel.width = dp(200)