Toolbar#
See also Material Design spec, App bars: top Material Design spec, App bars: bottom KivyMD provides the following bar positions for use: You can add MDTooltips to the icons by adding a text string to the bar item,
as shown below:
TopAppBar#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDBoxLayout:
orientation: "vertical"
md_bg_color: "#1E1E15"
MDTopAppBar:
title: "MDTopAppBar"
MDLabel:
text: "Content"
halign: "center"
'''
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()
Change bar color#
MDTopAppBar:
title: "MDTopAppBar"
anchor_title: "left"
md_bg_color: "brown"
Change bar text color#
MDTopAppBar:
title: "MDTopAppBar"
anchor_title: "left"
specific_text_color: "white"
Shadow elevation control#
MDTopAppBar:
title: "Elevation 4"
anchor_title: "left"
elevation: 4
shadow_color: "brown"
BottomAppBar#
M2 style bottom app bar#
Usage#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDBoxLayout:
md_bg_color: "#1E1E15"
# Will always be at the bottom of the screen.
MDBottomAppBar:
MDTopAppBar:
title: "MDBottomAppBar"
icon: "git"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
'''
class Example(MDApp):
def build(self):
self.theme_cls.material_style = "M2"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
Example().run()
Custom color#
MDBottomAppBar:
MDTopAppBar:
title: "MDBottomAppBar"
icon: "git"
type: "bottom"
left_action_items: [["menu", lambda x: x]]
icon_color: 0, 1, 0, 1
md_bg_bottom_color: "brown"
M3 style bottom app bar#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
MDFloatLayout:
md_bg_color: "#151511"
MDBottomAppBar:
md_bg_color: "#232217"
icon_color: "#8A8D79"
MDFabBottomAppBarButton:
icon: "plus"
md_bg_color: "#373A22"
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
Add action items#
#:import MDActionBottomAppBarButton kivymd.uix.toolbar.MDActionBottomAppBarButton
MDFloatLayout:
MDBottomAppBar:
action_items:
[
MDActionBottomAppBarButton(icon="gmail"),
MDActionBottomAppBarButton(icon="label-outline"),
MDActionBottomAppBarButton(icon="bookmark"),
]
Change action items#
from kivy.lang import Builder
from kivymd.app import MDApp
KV = '''
#:import MDActionBottomAppBarButton kivymd.uix.toolbar.MDActionBottomAppBarButton
MDFloatLayout:
md_bg_color: "#151511"
MDBottomAppBar:
id: bottom_appbar
md_bg_color: "#232217"
icon_color: "#8A8D79"
action_items:
[
MDActionBottomAppBarButton(icon="gmail"),
MDActionBottomAppBarButton(icon="bookmark"),
]
MDFabBottomAppBarButton:
icon: "plus"
md_bg_color: "#373A22"
on_release: app.change_actions_items()
'''
class Example(MDApp):
def change_actions_items(self):
self.root.ids.bottom_appbar.action_items = [
MDActionBottomAppBarButton(icon="magnify"),
MDActionBottomAppBarButton(icon="trash-can-outline"),
MDActionBottomAppBarButton(icon="download-box-outline"),
]
def build(self):
self.theme_cls.theme_style = "Dark"
return Builder.load_string(KV)
Example().run()
A practical example#
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.properties import StringProperty, BooleanProperty, ObjectProperty
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.toolbar import MDActionBottomAppBarButton
from kivymd.app import MDApp
from kivymd.utils import asynckivy
from faker import Faker # pip install Faker
KV = '''
#:import MDFabBottomAppBarButton kivymd.uix.toolbar.MDFabBottomAppBarButton
<UserCard>
orientation: "vertical"
adaptive_height: True
md_bg_color: "#373A22" if self.selected else "#1F1E15"
radius: 16
padding: 0, 0, 0, "16dp"
TwoLineAvatarListItem:
divider: None
_no_ripple_effect: True
text: root.name
secondary_text: root.time
theme_text_color: "Custom"
text_color: "#8A8D79"
secondary_theme_text_color: self.theme_text_color
secondary_text_color: self.text_color
ImageLeftWidget:
source: root.avatar
radius: self.height / 2
MDLabel:
text: root.text
adaptive_height: True
theme_text_color: "Custom"
text_color: "#8A8D79"
padding_x: "16dp"
shorten: True
shorten_from: "right"
Widget:
MDFloatLayout:
md_bg_color: "#151511"
RecycleView:
id: card_list
viewclass: "UserCard"
SelectableRecycleGridLayout:
orientation: 'vertical'
spacing: "16dp"
padding: "16dp"
default_size: None, dp(120)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
multiselect: True
touch_multiselect: True
MDBottomAppBar:
id: bottom_appbar
scroll_cls: card_list
allow_hidden: True
md_bg_color: "#232217"
icon_color: "#8A8D79"
MDFabBottomAppBarButton:
id: fab_button
icon: "plus"
md_bg_color: "#373A22"
'''
class UserCard(RecycleDataViewBehavior, MDBoxLayout):
name = StringProperty()
time = StringProperty()
text = StringProperty()
avatar = StringProperty()
callback = ObjectProperty(lambda x: x)
index = None
selected = BooleanProperty(False)
selectable = BooleanProperty(True)
def refresh_view_attrs(self, rv, index, data):
self.index = index
return super().refresh_view_attrs(rv, index, data)
def on_touch_down(self, touch):
if super().on_touch_down(touch):
return True
if self.collide_point(*touch.pos) and self.selectable:
Clock.schedule_once(self.callback)
return self.parent.select_with_touch(self.index, touch)
def apply_selection(self, rv, index, is_selected):
self.selected = is_selected
rv.data[index]["selected"] = is_selected
class SelectableRecycleGridLayout(
FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout
):
pass
class Test(MDApp):
selected_cards = False
def build(self):
return Builder.load_string(KV)
def on_tap_card(self, *args):
datas = [data["selected"] for data in self.root.ids.card_list.data]
if True in datas and not self.selected_cards:
self.root.ids.bottom_appbar.action_items = [
MDActionBottomAppBarButton(icon="gmail"),
MDActionBottomAppBarButton(icon="label-outline"),
MDActionBottomAppBarButton(icon="bookmark"),
]
self.root.ids.fab_button.icon = "pencil"
self.selected_cards = True
else:
if len(list(set(datas))) == 1 and not list(set(datas))[0]:
self.selected_cards = False
if not self.selected_cards:
self.root.ids.bottom_appbar.action_items = [
MDActionBottomAppBarButton(icon="magnify"),
MDActionBottomAppBarButton(icon="trash-can-outline"),
MDActionBottomAppBarButton(icon="download-box-outline"),
]
self.root.ids.fab_button.icon = "plus"
def on_start(self):
async def generate_card():
for i in range(10):
await asynckivy.sleep(0)
self.root.ids.card_list.data.append(
{
"name": fake.name(),
"time": fake.date(),
"avatar": fake.image_url(),
"text": fake.text(),
"selected": False,
"callback": self.on_tap_card,
}
)
self.on_tap_card()
fake = Faker()
Clock.schedule_once(lambda x: asynckivy.start(generate_card()))
Test().run()
Tooltips#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.snackbar import Snackbar
KV = '''
MDBoxLayout:
orientation: "vertical"
MDTopAppBar:
title: "MDTopAppBar"
left_action_items: [["menu", "This is the navigation"]]
right_action_items:
[
[
"dots-vertical",
lambda x: app.callback(x),
"this is the More Actions"
]
]
MDLabel:
text: "Content"
halign: "center"
'''
class Example(MDApp):
def build(self):
self.theme_cls.material_style = "M2"
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
def callback(self, button):
Snackbar(text="Hello World").open()
Example().run()
M3 style top app bar#
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.toolbar import MDTopAppBar
KV = '''
MDScreen:
MDBoxLayout:
id: box
orientation: "vertical"
spacing: "12dp"
pos_hint: {"top": 1}
adaptive_height: True
'''
class Example(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Orange"
return Builder.load_string(KV)
def on_start(self):
for type_height in ["medium", "large", "small"]:
self.root.ids.box.add_widget(
MDTopAppBar(
type_height=type_height,
headline_text=f"Headline {type_height.lower()}",
md_bg_color="brown",
left_action_items=[["arrow-left", lambda x: x]],
right_action_items=[
["attachment", lambda x: x],
["calendar", lambda x: x],
["dots-vertical", lambda x: x],
],
title="Title" if type_height == "small" else "",
anchor_title="left",
)
)
Example().run()
API - kivymd.uix.toolbar.toolbar
#
- class kivymd.uix.toolbar.toolbar.MDFabBottomAppBarButton(*args, **kwargs)#
Implements a floating action button (FAB) for a bar with type ‘bottom’.
For more information, see in the
MDFloatingActionButton
andRotateBehavior
andScaleBehavior
andMDTooltip
classes documentation.- set__radius(self, *args)#
- class kivymd.uix.toolbar.toolbar.MDActionBottomAppBarButton(*args, **kwargs)#
Implements action buttons for a :class:’MDBottomAppBar’ class.
New in version 1.2.0.
For more information, see in the
MDIconButton
andMDTooltip
classes documentation.
- class kivymd.uix.toolbar.toolbar.MDActionOverFlowButton(*args, **kwargs)#
Implements a bar action button for the OverFlowMenu menu.
For more information, see in the
MDIconButton
andMDTooltip
classes documentation.- icon = dots-vertical#
- class kivymd.uix.toolbar.toolbar.MDTopAppBar(**kwargs)#
Top app bar class.
For more information, see in the
DeclarativeBehavior
andNotchedBox
andWindowController
classes documentation.- Events:
- on_action_button
Method for the button used for the
MDBottomAppBar
class.
- left_action_items#
The icons on the left of the bar. To add one, append a list like the following:
MDTopAppBar: left_action_items: ["dots-vertical", callback, "tooltip text", "overflow text"]
icon_name
- is a string that corresponds to an icon definition:MDTopAppBar: right_action_items: [["home"]]
callback
- is the function called on a touch release event and:MDTopAppBar: right_action_items: [["home", lambda x: app.callback(x)]]
class Test(MDApp): def callback(self, instance_action_top_appbar_button): print(instance_action_top_appbar_button)
tooltip text
- is the text to be displayed in the tooltip:MDTopAppBar: right_action_items: [ ["home", lambda x: app.callback(x), "Home"], ["message-star", lambda x: app.callback(x), "Message star"], ["message-question", lambda x: app.callback(x), "Message question"], ["message-reply", lambda x: app.callback(x), "Message reply"], ]
overflow text
- is the text for menu items (OverFlowMenuItem
) of the corresponding action buttons:MDTopAppBar: use_overflow: True right_action_items: [ ["home", lambda x: x, "", "Home"], ["message-star", lambda x: x, "", "Message star"], ["message-question", lambda x: x, "" , "Message question"], ["message-reply", lambda x: x, "", "Message reply"], ]
icon color
- icon color:MDTopAppBar: right_action_items: [ [ "dots-vertical", callback, "tooltip text", "overflow text", (1, 1, 1, 1), ] ]
Both the
callback
andtooltip text
andoverflow text
andicon color
are optional but the order must be preserved.left_action_items
is anListProperty
and defaults to [].
- right_action_items#
The icons on the left of the bar. Works the same way as
left_action_items
.right_action_items
is anListProperty
and defaults to [].
- title#
Text app bar.
MDTopAppBar: title: "MDTopAppBar"
title
is anStringProperty
and defaults to ‘’.
- mode#
Floating button position. Only for
MDBottomAppBar
class. Available options are: ‘free-end’, ‘free-center’, ‘end’, ‘center’.Mode “end”:
MDBottomAppBar: MDTopAppBar: title: "Title" icon: "git" type: "bottom" left_action_items: [["menu", lambda x: x]] mode: "end"
Mode “free-end”:
MDBottomAppBar: MDTopAppBar: mode: "free-end"
Mode “free-center”:
MDBottomAppBar: MDTopAppBar: mode: "free-center"
Mode “center”:
MDBottomAppBar: MDTopAppBar: mode: "center"
mode
is anOptionProperty
and defaults to ‘center’.
- type#
When using the
MDBottomAppBar
class, the parametertype
must be set to ‘bottom’:MDBottomAppBar: MDTopAppBar: type: "bottom"
Available options are: ‘top’, ‘bottom’.
type
is anOptionProperty
and defaults to ‘top’.
- opposite_colors#
Changes the color of the label to the color opposite to the main theme.
MDTopAppBar: opposite_colors: True
MDTopAppBar: opposite_colors: False
- md_bg_bottom_color#
The background color in (r, g, b, a) or string format for the bar with the
bottom
mode.New in version 1.0.0.
MDBottomAppBar: MDTopAppBar: md_bg_bottom_color: "brown" icon_color: self.md_bg_bottom_color
md_bg_bottom_color
is anColorProperty
and defaults to None.
- set_bars_color#
If True the background color of the bar status will be set automatically according to the current color of the bar.
New in version 1.0.0.
See set_bars_colors for more information.
set_bars_color
is anBooleanProperty
and defaults to False.
- use_overflow#
As a top app bar is resized, actions move to the overflow menu from right to left.
New in version 1.0.0.
MDTopAppBar: title: "MDTopAppBar" use_overflow: True right_action_items: [ ["home", lambda x: x, "Home", "Home"], ["message-star", lambda x: x, "Message star", "Message star"], ["message-question", lambda x: x, "Message question", "Message question"], ["message-reply", lambda x: x, "Message reply", "Message reply"], ]
use_overflow
is anBooleanProperty
and defaults to False.
- overflow_cls#
Must be an object of the
MDDropdownMenu
class documentation for more information.New in version 1.0.0.
from kivy.lang import Builder from kivymd.app import MDApp from kivymd.uix.menu import MDDropdownMenu KV = ''' #:import CustomOverFlowMenu __main__.CustomOverFlowMenu MDBoxLayout: orientation: "vertical" MDTopAppBar: title: "MDTopAppBar" use_overflow: True overflow_cls: CustomOverFlowMenu() right_action_items: [ ["home", lambda x: x, "Home", "Home"], ["message-star", lambda x: x, "Message star", "Message star"], ["message-question", lambda x: x, "Message question", "Message question"], ["message-reply", lambda x: x, "Message reply", "Message reply"], ] MDLabel: text: "Content" halign: "center" ''' class CustomOverFlowMenu(MDDropdownMenu): # In this class you can set custom properties for the overflow menu. pass class Example(MDApp): def build(self): return Builder.load_string(KV) def callback(self, instance_action_top_appbar_button): print(instance_action_top_appbar_button) Example().run()
overflow_cls
is anObjectProperty
and defaults to None.
- icon#
Floating button. Only for
MDBottomAppBar
class.icon
is anStringProperty
and defaults to ‘android’.
- icon_color#
Color in (r, g, b, a) or string format action button. Only for
MDBottomAppBar
class.icon_color
is anColorProperty
and defaults to [].
- anchor_title#
Position bar title. Only used with material_style = ‘M3’ Available options are: ‘left’, ‘center’, ‘right’.
anchor_title
is anOptionProperty
and defaults to None.
- headline_text#
Headline text bar.
New in version 1.0.0.
headline_text
is anStringProperty
and defaults to ‘’.
- headline_text_color#
Headline text color in (r, g, b, a) or string format.
New in version 1.0.0.
headline_text_color
is anColorProperty
and defaults to None.
- type_height#
Bar height type.
New in version 1.0.0.
Available options are: ‘medium’, ‘large’, ‘small’.
type_height
is anOptionProperty
and defaults to ‘small’.
- on_width(self, instance_toolbar, width: float)#
Called when the bar is resized (size of the application window).
- return_action_button_to_toolbar(self)#
- remove_overflow_button(self)#
Removes an overflow button to the bar.
- add_overflow_button(self)#
Adds an overflow button to the bar.
- overflow_action_button_is_added(self)#
Returns True if at least one action button (:class:`~ActionTopAppBarButton’) on the bar is added to the overflow.
- add_action_button_to_overflow(self)#
Adds an overflow button to the bar.
- check_overflow_cls(self, interval: int | float)#
If the user does not set the
overflow_cls
attribute but uses overflows, theoverflow_cls
attribute will use the default value.
- on_type(self, instance_toolbar, type_value: str)#
Called when the value of the
type
attribute changes.
- on_type_height(self, instance_toolbar, height_type_value: str)#
Called when the value of the
type_height
attribute changes.
- on_action_button(self, *args)#
Method for the button used for the
MDBottomAppBar
class.
- on_overflow_cls(self, instance_toolbar, instance_overflow_cls: MDDropdownMenu)#
Called when the value of the
overflow_cls
attribute changes.
- on_md_bg_color(self, instance_toolbar, color_value: list)#
Called when the value of the
md_bg_color
attribute changes.
- on_left_action_items(self, instance_toolbar, items_value: list)#
Called when the value of the
left_action_items
attribute changes.
- on_right_action_items(self, instance_toolbar, items_value: list)#
Called when the value of the
right_action_items
attribute changes.
- on_icon(self, instance_toolbar, icon_name: str)#
Called when the value of the
icon
attribute changes.
- on_icon_color(self, instance, icon_name: str)#
Called when the value of the
icon_color
attribute changes.
- on_md_bg_bottom_color(self, instance_toolbar, color_value: list)#
Called when the value of the
md_bg_bottom_color
attribute changes.
- on_anchor_title(self, instance_toolbar, anchor_value: str)#
Called when the value of the
anchor_title
attribute changes.
- on_mode(self, instance_toolbar, mode_value: str)#
Called when the value of the
made
attribute changes.
- set_notch(self)#
- set_shadow(self, *args)#
- get_default_overflow_cls(self)#
- update_md_bg_color(self, *args)#
- update_action_bar_text_colors(self, *args)#
- remove_notch(self)#
- remove_shadow(self)#
- class kivymd.uix.toolbar.toolbar.MDBottomAppBar(*args, **kwargs)#
Bottom app bar class.
For more information, see in the
DeclarativeBehavior
andThemableBehavior
andSpecificBackgroundColorBehavior
andCommonElevationBehavior
andFloatLayout
classes documentation.- Events:
- on_show_bar
The method is called when the
MDBottomAppBar
panel is shown.- on_hide_bar
The method is called when the
MDBottomAppBar
panel is hidden.
- md_bg_color#
Color bar in (r, g, b, a) or string format.
md_bg_color
is anColorProperty
and defaults to [0, 0, 0, 0].
- icon_color#
Color bar in (r, g, b, a) or string format.
New in version 1.2.0.
icon_color
is anColorProperty
and defaults to None.
- action_items#
The icons on the left bar.
New in version 1.2.0.
action_items
is anListProperty
and defaults to [].
- animation#
# TODO: add description. # FIXME: changing the value does not affect anything.
New in version 1.2.0.
animation
is anBooleanProperty
and defaults to True.
- show_transition#
Type of button display transition.
New in version 1.2.0.
show_transition
is aStringProperty
and defaults to ‘linear’.
- hide_transition#
Type of button hidden transition.
New in version 1.2.0.
hide_transition
is aStringProperty
and defaults to ‘in_back’.
- hide_duration#
Duration of button hidden transition.
New in version 1.2.0.
hide_duration
is aNumericProperty
and defaults to 0.2.
- show_duration#
Duration of button display transition.
New in version 1.2.0.
show_duration
is aNumericProperty
and defaults to 0.2.
- scroll_cls#
Widget inherited from the
ScrollView
class. The value must be set if theallow_hidden
parameter is True.New in version 1.2.0.
scroll_cls
is aObjectProperty
and defaults to None.
Allows or disables hiding the panel when scrolling content. If the value is True, the
scroll_cls
parameter must be specified.New in version 1.2.0.
allow_hidden
is aBooleanProperty
and defaults to False.
Is the panel currently hidden.
New in version 1.2.0.
bar_is_hidden
is aBooleanProperty
and defaults to False.
- button_centering_animation(self, button: MDActionOverFlowButton | MDActionBottomAppBarButton | MDFabBottomAppBarButton)#
Animation of centering buttons for
MDActionOverFlowButton
,MDActionBottomAppBarButton
andMDFabBottomAppBarButton
classes.
- check_scroll_direction(self, scroll_cls, y: float)#
Checks the scrolling direction. Depending on the scrolling direction, hides or shows the
MDBottomAppBar
panel.
- show_bar(self)#
Show
MDBottomAppBar
panel.
- hide_bar(self)#
Hide
MDBottomAppBar
panel.
- on_show_bar(self, *args)#
The method is called when the
MDBottomAppBar
panel is shown.
- on_hide_bar(self, *args)#
The method is called when the
MDBottomAppBar
panel is hidden.
- on_scroll_cls(self, instance, scroll_cls)#
Called when the value of the
scroll_cls
attribute changes.
- on_size(self, *args)#
Called when the root screen is resized.
- on_action_items(self, instance, value: list)#
Called when the value of the
action_items
attribute changes.
- set_fab_opacity(self, *ars)#
Sets the transparency value of the:class:~MDFabBottomAppBarButton button.
- set_fab_icon(self, instance, value)#
Animates the size of the
MDFabBottomAppBarButton
button.
- set_bg_color(self, *args)#
Sets the background color for the
MDBottomAppBar
class.
- set_icon_color(self, widget: MDActionOverFlowButton | MDActionBottomAppBarButton)#
Sets the icon color for the
MDActionOverFlowButton
andMDActionBottomAppBarButton
classes.
- add_widget(self, widget, index=0, canvas=None)#
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.
- widget:
>>> from kivy.uix.button import Button >>> from kivy.uix.slider import Slider >>> root = Widget() >>> root.add_widget(Button()) >>> slider = Slider() >>> root.add_widget(slider)