Snackbar#

Snackbars provide brief messages about app processes at the bottom of the screen.

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar.png

Usage#

MDSnackbar(
    MDLabel(
        text="First string",
        theme_text_color="Custom",
        text_color="#393231",
    ),
).open()

Example#

from kivy.lang import Builder

from kivymd.app import MDApp
from kivymd.uix.label import MDLabel
from kivymd.uix.snackbar import MDSnackbar


KV = '''
MDScreen:

    MDRaisedButton:
        text: "Create simple snackbar"
        on_release: app.open_snackbar()
        pos_hint: {"center_x": .5, "center_y": .5}
'''


class Example(MDApp):
    def open_snackbar(self):
        MDSnackbar(
            MDLabel(
                text="First string",
            ),
        ).open()

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


Example().run()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-simple.gif

Control width and pos#

MDSnackbar(
    MDLabel(
        text="First string",
    ),
    pos=(dp(24), dp(56)),
    size_hint_x=0.5,
).open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-widith-and-pos.gif

On mobile, use up to two lines of text to communicate the snackbar message:

MDSnackbar(
    MDLabel(
        text="First string",
        theme_text_color="Custom",
        text_color="#393231",
    ),
    MDLabel(
        text="Second string",
        theme_text_color="Custom",
        text_color="#393231",
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    md_bg_color="#E8D8D7",
).open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-two-line.gif

Usage action button#

A snackbar can contain a single action. “Dismiss” or “cancel” actions are optional:

MDSnackbar(
    MDLabel(
        text="First string",
        theme_text_color="Custom",
        text_color="#393231",
    ),
    MDSnackbarActionButton(
        text="Done",
        theme_text_color="Custom",
        text_color="#8E353C",
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    md_bg_color="#E8D8D7",
).open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-action-button.gif

Callback action button#

def snackbar_action_button_callback(self, *args):
    print("Snackbar callback action button")

def open_snackbar(self):
    self.snackbar = MDSnackbar(
        MDLabel(
            text="First string",
            theme_text_color="Custom",
            text_color="#393231",
        ),
        MDSnackbarActionButton(
            text="Done",
            theme_text_color="Custom",
            text_color="#8E353C",
            _no_ripple_effect=True,
            on_release=self.snackbar_action_button_callback,
        ),
        y=dp(24),
        pos_hint={"center_x": 0.5},
        size_hint_x=0.5,
        md_bg_color="#E8D8D7",
    )
    self.snackbar.open()

If an action is long, it can be displayed on a third line:

MDSnackbar(
    MDLabel(
        text="If an action is long, it can be displayed",
        theme_text_color="Custom",
        text_color="#393231",
    ),
    MDLabel(
        text="on a third line.",
        theme_text_color="Custom",
        text_color="#393231",
    ),
    MDLabel(
        text=" ",
    ),
    MDSnackbarActionButton(
        text="Action button",
        theme_text_color="Custom",
        text_color="#8E353C",
        y=dp(8),
        _no_ripple_effect=True,
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    md_bg_color="#E8D8D7",
).open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-action-button-on-thrid-line.gif

Icon (optional close affordance):

def snackbar_close(self, *args):
    self.snackbar.dismiss()

def open_snackbar(self):
    self.snackbar = MDSnackbar(
        MDLabel(
            text="Icon (optional close affordance)",
            theme_text_color="Custom",
            text_color="#393231",
        ),
        MDSnackbarActionButton(
            text="Action button",
            theme_text_color="Custom",
            text_color="#8E353C",
            _no_ripple_effect=True,
        ),
        MDSnackbarCloseButton(
            icon="close",
            theme_text_color="Custom",
            text_color="#8E353C",
            _no_ripple_effect=True,
            on_release=self.snackbar_close,
        ),
        y=dp(24),
        pos_hint={"center_x": 0.5},
        size_hint_x=0.5,
        md_bg_color="#E8D8D7",
    )
    self.snackbar.open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snackbar-optional-close-affordance.gif

API break#

1.1.1 version#

snackbar = Snackbar(
    text="First string",
    snackbar_x="10dp",
    snackbar_y="24dp",
)
snackbar.size_hint_x = (
    Window.width - (snackbar.snackbar_x * 2)
) / Window.width
snackbar.buttons = [
    MDFlatButton(
        text="Done",
        theme_text_color="Custom",
        text_color="#8E353C",
        on_release=snackbar.dismiss,
    ),
]
snackbar.open()

1.2.0 version#

MDSnackbar(
    MDLabel(
        text="First string",
    ),
    MDSnackbarActionButton(
        text="Done",
        theme_text_color="Custom",
        text_color="#8E353C",
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    md_bg_color="#E8D8D7",
).open()

API - kivymd.uix.snackbar.snackbar#

class kivymd.uix.snackbar.snackbar.MDSnackbarCloseButton(*args, **kwargs)#

Snackbar closed button class.

For more information, see in the MDIconButton class documentation.

class kivymd.uix.snackbar.snackbar.MDSnackbarActionButton(*args, **kwargs)#

Snackbar action button class.

For more information, see in the MDFlatButton class documentation.

class kivymd.uix.snackbar.snackbar.MDSnackbar(*args, **kwargs)#

Snackbar class.

Changed in version 1.2.0: Rename BaseSnackbar to MDSnackbar class.

For more information, see in the MDCard and StencilBehavior class documentation.

Events:
on_open

Called when a snackbar opened.

on_dismiss

Called when a snackbar closes.

duration#

The amount of time that the snackbar will stay on screen for.

duration is a NumericProperty and defaults to 3.

auto_dismiss#

Whether to use automatic closing of the snackbar or not.

auto_dismiss is a BooleanProperty and defaults to True.

radius#

Snackbar radius.

radius is a ListProperty and defaults to [5, 5, 5, 5]

bg_color#

Snackbar background color in (r, g, b, a) or string format.

Deprecated since version 1.2.0: Use ‘md_bg_color` instead.

bg_color is a ColorProperty and defaults to None.

buttons#

Snackbar buttons.

Deprecated since version 1.2.0.

buttons is a ListProperty and defaults to []

snackbar_animation_dir#

Snackbar animation direction. Available options are: ‘Top’, ‘Bottom’, ‘Left’, ‘Right’.

Deprecated since version 1.2.0.

snackbar_animation_dir is an OptionProperty and defaults to ‘Bottom’.

snackbar_x#

The snackbar x position in the screen

Deprecated since version 1.2.0.

snackbar_x is a NumericProperty and defaults to 0.

snackbar_y#

The snackbar x position in the screen

Deprecated since version 1.2.0.

snackbar_y is a NumericProperty and defaults to 0.

dismiss(self, *args)#

Dismiss the snackbar.

open(self)#

Show the snackbar.

add_widget(self, widget, *args, **kwargs)#

Add a new widget as a child of this widget.

Parameters:
widget: Widget

Widget to add to our list of children.

index: int, defaults to 0

Index to insert the widget in the list. Notice that the default of 0 means the widget is inserted at the beginning of the list and will thus be drawn on top of other sibling widgets. For a full discussion of the index and widget hierarchy, please see the Widgets Programming Guide.

New in version 1.0.5.

canvas: str, defaults to None

Canvas to add widget’s canvas to. Can be ‘before’, ‘after’ or None for the default canvas.

New in version 1.9.0.

>>> from kivy.uix.button import Button
>>> from kivy.uix.slider import Slider
>>> root = Widget()
>>> root.add_widget(Button())
>>> slider = Slider()
>>> root.add_widget(slider)
on_open(self, *args)#

Called when a snackbar opened.

on_dismiss(self, *args)#

Called when a snackbar closed.