Change toolbar navigationIcon tint or color via theme or style [duplicate]
Asked Answered
Z

2

8

I can set up navigation icon with tint in toolbar from code:

  Toolbar toolbar = findViewById(R.id.biometry_agreement_toolbar);
  Drawable drawable = AppCompatResources.getDrawable(requireContext(), R.drawable.ic_24_close);
  drawable.setColorFilter(ColorGenerator.buildColorFilterByAttr(toolbar.getContext(), R.attr.colorMaskColored));
  toolbar.setNavigationIcon(drawable);

or I can set icon like this in xml, but in this case i can't set tint since there is no attribute navigationIconTint:

<androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:navigationIcon="@drawable/ic_24_close"
            app:titleTextColor="@color/color_primary"
            tools:title="@string/title"/>

Is it possible to somehow set a tint for navigation icon, without java code? or setting custom icon into toolbar clears all tints and colours?

I don't like setting icons from java because there is no nice code reuse for toolbar initialisation.

I've tried different custom styles, overriding colorControlNormal, but no luck.

Thanks in advance

Zacheryzack answered 26/9, 2019 at 5:36 Comment(0)
D
10

For AndroidX you should use <com.google.android.material.appbar.MaterialToolbar instead of <androidx.appcompat.widget.Toolbar

Then try this,

<com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/mt_main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:elevation="1dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

Use it likethis

mt_main_toolbar.navigationIcon.setTint(Color.RED)

Using XML

<style name="TintedNavigation" parent="Widget.AppCompat.Toolbar.Button.Navigation">
<item name="tint">@color/nav_button_tint</item>

and use it in your style

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="toolbarNavigationButtonStyle">@style/TintedNavigation</item>
</style>

For AndroidX

<style name="CustomToolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="colorOnPrimary">@color/yourColour</item>
</style>
Dorisdorisa answered 26/9, 2019 at 5:46 Comment(7)
I know how to set tint with java/kotlin. But I want to somehow use themes and styles as a solution. If it is possible in such case.Zacheryzack
@MaksimTuraev see the updated answerDorisdorisa
It is wrong. Since you are using the Material Components library, you shouldn't use an AppCompat theme. Check the doc.Waylan
@GabrieleMariotti I posted that because he was using old components, i'll add the new tooDorisdorisa
Why should you use the MaterialToolbar instead of AndroidX Toolbar?Samal
@Samal It's recommended to use Material Toolbar because it has got some more properties than the other toolbarDorisdorisa
THIS is just what i needed, excelent answer.Gabriella
L
-2

Please Refer Below Answer How to change Toolbar home icon color

Try Below Code But It May Affect The Whole Theme Color Like Radio Button Etc.,

@color/colorControlNormal

Leatherwood answered 26/9, 2019 at 5:46 Comment(5)
no luck, I've created style <style name="ThemeOverlay.Sbrf.Light.CustomToolbar"> <item name="android:textColorSecondary">@color/color_yellow_5</item> </style> and referenced it from toolbar like this style="@style/ThemeOverlay.Sbrf.Light.CustomToolbar" Icon unfortunately still blackZacheryzack
Try This <style name="Theme.MyFancyTheme" parent="android:Theme.Holo"> <item name="android:homeAsUpIndicator">@drawable/back_button_image</item> </style> Or <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.ActionBar"> <!-- Customize color of navigation drawer icon and back arrow --> <item name="colorControlNormal">@color/toolbar_color_control_normal</item> </style>Leatherwood
@VaithiyaNathan Holo theme in 2019?Waylan
@gabriele-mariotti That's Just For Reference Use The Below Code ANd Change Color In "colorControlNormal" <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowDisablePreview">true</item> <item name="colorControlNormal">@color/colorAccent</item> </style>Leatherwood
@VaithiyaNathan changing the attribute at app theme level means to change globally this value for all components. You can just override the theme only for the toolbar. Also the Material Components uses the colorOnPrimary for the toolbar icon. Check the answer linked in the original question.Waylan

© 2022 - 2024 — McMap. All rights reserved.