Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 232
Maksim Turaev Avatar asked Sep 26 '19 05:09

Maksim Turaev


People also ask

What is Material toolbar?

MaterialToolbar is a Toolbar that implements certain Material features, such as elevation overlays for Dark Themes and centered titles.


1 Answers

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>
like image 142
Manoj Perumarath Avatar answered Sep 28 '22 04:09

Manoj Perumarath