Home > Software design >  How can I swipeable Kivy Button that can also have single, long or double press functionality?
How can I swipeable Kivy Button that can also have single, long or double press functionality?

Time:01-03

I want to make Kivy mobile app that has similar swipeable Buttons (or other purpose filling widgets). In adition to that I want to have seccond type of input from same Button. For instance Gmail mobile app uses this feature:

enter image description here

I have this code:

main.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class ItemList(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.orientation = "vertical"
        self.item_list = []
        for i in range(10):
            self.item_list.append(Button(text=str(i),
                                    on_release=lambda x, this_item=i: print(this_item)))
            self.add_widget(self.item_list[-1])

class MainApp(App):
    pass

MainApp().run()

main.kv

ItemList:

I want the program to be able to understand witch button on the list is pressed and is the press "type" a swipe or single/double/long press (any one of three will do) and excecute corresponding method.

I was looking for pre built package for this, but I maybe using the wrong keywords, since I cannot find any to suit this type of Buttons/widgets on a list aproach.

CodePudding user response:

The following explanations or suggestions are based on the fact that you are new (as it seems) to Kivy.

As I understood until now, in kivy you perhaps can not expect something as ready-to-use component. But this, rather than being a downside, actually is a very effective way to let the developer choose, implement their own. There are numerous ways to program a CustomWidget, say a Button. You can change the appearance, touch feedback, texture etc. to name a few.

I want the program to be able to understand witch button on the list is pressed and is the press "type" a swipe or single/double/long press (any one of three will do) and excecute corresponding method.

To get a touch behaviour (behavior) of your own, aside from the default mixin ButtonBehavior, you can implement the required logic with three touch events on_touch_down, on_touch_move and on_touch_up. To enable a long press/ hold behaviour one way can be scheduling an action in on_touch_down event with a time threshold beyond which the action will be fired via the same event. Again to get a swipe behaviour you can implement the logic within on_touch_move event and in others' as necessary. You can also restrict the touch event to propagate further by checking collide_point, collide_widget etc.

You can track/identify any Button with its ids or by passing some argument(s) through it.

I was looking for pre built package for this, but I maybe using ...

Yes, there are some. KivyMD is one of these projects developed based on Kivy that approximates the Google's Material Design.

  • Related