Search code examples
kivykivy-languagekivymd

How to put MDDropdownMenu over MDlist item in KivyMD?


I have one ScrollView under that I have MDList and I want that 'When I click on MDlist item MDDropdownMenu should open exactly over that item'.

ScrollView:
    pos_hint: {'center_x': .5, 'center_y': .4}
    MDList:
        pos_hint: {'center_x': .5, 'center_y': .85}
        id: box

Solution

  • from kivy.lang import Builder
    
    from kivymd.app import MDApp
    from kivymd.uix.list import OneLineListItem
    from kivymd.uix.menu import MDDropdownMenu
    
    KV = """
    ScrollView:
    
        MDList:
            id: box
            cols: 1
    """
    
    
    class Test(MDApp):
        def build(self):
            return Builder.load_string(KV)
    
        def show_menu(self, instance):
            menu_items = [{"text": f"Item {i}"} for i in range(5)]
            menu = MDDropdownMenu(caller=instance, items=menu_items, width_mult=4
            )
            menu.open()
    
        def on_start(self):
            for i in range(20):
                self.root.ids.box.add_widget(
                    OneLineListItem(text=f"Item {i}", on_release=self.show_menu)
                )
    
    
    Test().run()