WillingLearner
WillingLearner

Reputation: 7316

change color of icon in MDToolbar in kivyMD?

I want to change the MDToolbar left_action_item icon color. Its defaulting to white, but now i want to change it to red. Whats the simplest way to do this? I've tried almost everything (text_color, bg_color, etc) all to no avail.

Upvotes: 3

Views: 6412

Answers (4)

idcabnun
idcabnun

Reputation: 21

Using both md_bg_color: app.theme_cls.primary_color and text_color: rgba('#F0F0F0') allowed me to change the color of icon buttons within MDToolbar.

Upvotes: 0

quadrismegistus
quadrismegistus

Reputation: 101

In situations like these, I recommend searching the KivyMD repository for the relevant widget class, and then poking around to see how it's being defined, what the relevant IDs are, and so forth. For instance, this line in toolbar.py seems to define the icons in the toolbar:

def update_action_bar(self, action_bar, action_bar_items):
    #...
    action_bar.add_widget(
        MDIconButton(
            icon=item[0],
            on_release=item[1],
            opposite_colors=True,
            text_color=self.specific_text_color,
            theme_text_color="Custom",
        )
    )
    #...

Here we learn that the toolbar's icons are of class MDIconButton, and they have a text_color color attribute which seems to be setting the color.

Looking at where the function above is called, we see that these icons are being added as widgets to self.ids["left_actions"] and self.ids["right_actions"] respectively:

def on_left_action_items(self, instance, value):
    self.update_action_bar(self.ids["left_actions"], value)

def on_right_action_items(self, instance, value):
    self.update_action_bar(self.ids["right_actions"], value)

Knowing all that, now in our own code, say in the build() function of our MainApp, we can access and modify the attribute:

def build(self):
    # ... 
        
    # get the root widget
    self.root = root = Builder.load_file('root.kv')

    # get toolbar
    toolbar=root.ids.toolbar
    
    # get the icons on the right
    action_items = toolbar.ids.right_actions.children

    # loop over the icons
    for item in action_items:
        # change the color
        item.text_color=(1,0,0,1) # red

This doesn't need to be in build(), it just needs to be somewhere you can access the toolbar widget by its ID somehow.

Upvotes: 2

Niek
Niek

Reputation: 35

Using specific_text_color: 1,0,1,1 you can change the color of the text inside the toolbar. It changes both the text AND the icon. I have no idea how to change only the icon. Maybe this helps.

At the moment iam having trouble changing the icon color of a OneLineIconListItem. I think its the same constraint we are encountering?

Upvotes: 1

Xyanight
Xyanight

Reputation: 1325

You cannot change the color of the icons in the toolbar.

Upvotes: 1

Related Questions