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
  • Snackbars shouldn’t interrupt the user’s experience

  • Usually appear at the bottom of the UI

  • Can disappear on their own or remain on screen until the user takes action

Usage#

MDSnackbar(
    MDSnackbarText(
        text="Text",
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
).open()
https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-anatomy-detail.png
  1. Container

  2. Supporting text

  3. Action (optional)

  4. Icon (optional close affordance)

Anatomy#

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

Configurations#

1. Single line#

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line.png
MDSnackbar(
    MDSnackbarText(
        text="Single-line snackbar",
    ),
    y=dp(24),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
).open()

2. Single-line snackbar with action#

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line-with-action.png
MDSnackbar(
    MDSnackbarSupportingText(
        text="Single-line snackbar with action",
    ),
    MDSnackbarButtonContainer(
        MDSnackbarActionButton(
            MDSnackbarActionButtonText(
                text="Action button"
            ),
        ),
        pos_hint={"center_y": 0.5}
    ),
    y=dp(24),
    orientation="horizontal",
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
).open()

3. Single-line snackbar with action and close buttons#

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-single-line-with-action-and-close-buttons.png
MDSnackbar(
    MDSnackbarSupportingText(
        text="Single-line snackbar with action and close buttons",
    ),
    MDSnackbarButtonContainer(
        MDSnackbarActionButton(
            MDSnackbarActionButtonText(
                text="Action button"
            ),
        ),
        MDSnackbarCloseButton(
            icon="close",
        ),
        pos_hint={"center_y": 0.5}
    ),
    y=dp(24),
    orientation="horizontal",
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
).open()

4. Two-line snackbar with action and close buttons#

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-two-line-with-action-and-close-buttons.png
MDSnackbar(
    MDSnackbarText(
        text="Single-line snackbar",
    ),
    MDSnackbarSupportingText(
        text="with action and close buttons",
    ),
    MDSnackbarButtonContainer(
        MDSnackbarActionButton(
            MDSnackbarActionButtonText(
                text="Action button"
            ),
        ),
        MDSnackbarCloseButton(
            icon="close",
        ),
        pos_hint={"center_y": 0.5}
    ),
    y=dp(24),
    orientation="horizontal",
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
).open()

5. Two-line snackbar with action and close buttons at the bottom#

https://github.com/HeaTTheatR/KivyMD-data/raw/master/gallery/kivymddoc/snakbar-configurations-two-line-with-action-and-close-buttons-bottom.png
MDSnackbar(
    MDSnackbarText(
        text="Single-line snackbar with action",
    ),
    MDSnackbarSupportingText(
        text="and close buttons at the bottom",
        padding=[0, 0, 0, dp(56)],
    ),
    MDSnackbarButtonContainer(
        Widget(),
        MDSnackbarActionButton(
            MDSnackbarActionButtonText(
                text="Action button"
            ),
        ),
        MDSnackbarCloseButton(
            icon="close",
        ),
    ),
    y=dp(124),
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    padding=[0, 0, "8dp", "8dp"],
).open()

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()

2.0.0 version#

MDSnackbar(
    MDSnackbarSupportingText(
        text="Single-line snackbar with action",
    ),
    MDSnackbarButtonContainer(
        MDSnackbarActionButton(
            MDSnackbarActionButtonText(
                text="Action button"
            ),
        ),
        pos_hint={"center_y": 0.5}
    ),
    y=dp(24),
    orientation="horizontal",
    pos_hint={"center_x": 0.5},
    size_hint_x=0.5,
    background_color=self.theme_cls.onPrimaryContainerColor,
).open()

API - kivymd.uix.snackbar.snackbar#

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

The class implements a container for placing snackbar buttons.

For more information, see in the DeclarativeBehavior and BoxLayout classes documentation.

add_widget(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.

Added 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.

Added 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)
class kivymd.uix.snackbar.snackbar.MDSnackbarCloseButton(**kwargs)#

Snackbar closed button class.

For more information, see in the MDIconButton class documentation.

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

The class implements the text for the MDSnackbarActionButton class.

Changed in version 2.2.0.

For more information, see in the MDButtonText class documentation.

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

Snackbar action button class.

For more information, see in the MDButton 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 MotionShackBehavior and MDCard and class documentation.

Events:
on_open

Fired when a snackbar opened.

on_dismiss

Fired 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 [dp(4), dp(4), dp(4), dp(4)]

background_color#

The background color in (r, g, b, a) or string format of the snackbar.

background_color is a ColorProperty and defaults to None.

dismiss(*args) None#

Dismiss the snackbar.

open() None#

Show the snackbar.

add_widget(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.

Added 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.

Added 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(*args) None#

Fired when a snackbar opened.

on_dismiss(*args) None#

Fired when a snackbar closed.

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

The class implements the text.

For more information, see in the MDLabel class documentation.

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

The class implements the supporting text.

For more information, see in the MDLabel class documentation.