Skip to content

Commit 0ed7c76

Browse files
pekingmeleticiarossi
authored andcommitted
[BottomAppBar] Adding the attr to set the color of navigation icon.
PiperOrigin-RevId: 370999656
1 parent 5c0b8d6 commit 0ed7c76

File tree

7 files changed

+47
-6
lines changed

7 files changed

+47
-6
lines changed

docs/components/BottomAppBar.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,10 @@ Element | Attribute | Related method(s)
224224

225225
### Navigation icon attributes
226226

227-
Element | Attribute | Related method(s) | Default value
228-
--------- | -------------------- | ------------------------------------------ | -------------
229-
**Icon** | `app:navigationIcon` | `setNavigationIcon`<br>`getNavigationIcon` | `null`
230-
**Color** | N/A | N/A | `?attr/colorControlNormal` (as `Drawable` tint)
227+
Element | Attribute | Related method(s) | Default value
228+
--------- | ------------------------ | ------------------------------------------ | -------------
229+
**Icon** | `app:navigationIcon` | `setNavigationIcon`<br>`getNavigationIcon` | `null`
230+
**Color** | `app:navigationIconTint` | `setNavigationIconTint` | `?attr/colorControlNormal` (as `Drawable` tint)
231231

232232
### FAB attributes
233233

lib/java/com/google/android/material/appbar/res-public/values/public.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
<public name="scrimVisibleHeightTrigger" type="attr"/>
6363
<public name="titleEnabled" type="attr"/>
6464
<public name="maxLines" type="attr"/>
65-
<public name="navigationIconTint" type="attr"/>
6665
<public name="titleCentered" type="attr"/>
6766
<public name="subtitleCentered" type="attr"/>
6867
</resources>

lib/java/com/google/android/material/appbar/res/values/attrs.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
<declare-styleable name="MaterialToolbar">
237237
<!-- The tint color for the navigation button icon drawable corresponding to
238238
the "app:navigationIcon" attribute. -->
239-
<attr name="navigationIconTint" format="color" />
239+
<attr name="navigationIconTint" />
240240
<!-- Whether the title text corresponding to the "app:title" attribute
241241
should be centered horizontally within the toolbar. Default is false.
242242
-->

lib/java/com/google/android/material/bottomappbar/BottomAppBar.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import android.graphics.Paint.Style;
3232
import android.graphics.Rect;
3333
import android.graphics.RectF;
34+
import android.graphics.drawable.Drawable;
3435
import android.os.Parcel;
3536
import android.os.Parcelable;
3637
import androidx.core.graphics.drawable.DrawableCompat;
@@ -44,6 +45,7 @@
4445
import android.view.Gravity;
4546
import android.view.View;
4647
import android.view.ViewGroup;
48+
import androidx.annotation.ColorInt;
4749
import androidx.annotation.Dimension;
4850
import androidx.annotation.IntDef;
4951
import androidx.annotation.MenuRes;
@@ -136,6 +138,7 @@ public class BottomAppBar extends Toolbar implements AttachedBehavior {
136138
@Retention(RetentionPolicy.SOURCE)
137139
public @interface FabAnimationMode {}
138140

141+
@Nullable private Integer navigationIconTint;
139142
private final int fabOffsetEndMode;
140143
private final MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
141144

@@ -252,6 +255,10 @@ public BottomAppBar(@NonNull Context context, @Nullable AttributeSet attrs, int
252255
ColorStateList backgroundTint =
253256
MaterialResources.getColorStateList(context, a, R.styleable.BottomAppBar_backgroundTint);
254257

258+
if (a.hasValue(R.styleable.BottomAppBar_navigationIconTint)) {
259+
setNavigationIconTint(a.getColor(R.styleable.BottomAppBar_navigationIconTint, -1));
260+
}
261+
255262
int elevation = a.getDimensionPixelSize(R.styleable.BottomAppBar_elevation, 0);
256263
float fabCradleMargin = a.getDimensionPixelOffset(R.styleable.BottomAppBar_fabCradleMargin, 0);
257264
float fabCornerRadius =
@@ -330,6 +337,25 @@ public WindowInsetsCompat onApplyWindowInsets(
330337
});
331338
}
332339

340+
@Override
341+
public void setNavigationIcon(@Nullable Drawable drawable) {
342+
super.setNavigationIcon(maybeTintNavigationIcon(drawable));
343+
}
344+
345+
/**
346+
* Sets the color of the toolbar's navigation icon.
347+
*
348+
* @see #setNavigationIcon
349+
*/
350+
public void setNavigationIconTint(@ColorInt int navigationIconTint) {
351+
this.navigationIconTint = navigationIconTint;
352+
Drawable navigationIcon = getNavigationIcon();
353+
if (navigationIcon != null) {
354+
// Causes navigation icon to be tinted if needed.
355+
setNavigationIcon(navigationIcon);
356+
}
357+
}
358+
333359
/**
334360
* Returns the current fabAlignmentMode, either {@link #FAB_ALIGNMENT_MODE_CENTER} or {@link
335361
* #FAB_ALIGNMENT_MODE_END}.
@@ -663,6 +689,17 @@ private void createFabTranslationXAnimation(
663689
animators.add(animator);
664690
}
665691

692+
@Nullable
693+
private Drawable maybeTintNavigationIcon(@Nullable Drawable navigationIcon) {
694+
if (navigationIcon != null && navigationIconTint != null) {
695+
Drawable wrappedNavigationIcon = DrawableCompat.wrap(navigationIcon);
696+
DrawableCompat.setTint(wrappedNavigationIcon, navigationIconTint);
697+
return wrappedNavigationIcon;
698+
} else {
699+
return navigationIcon;
700+
}
701+
}
702+
666703
private void maybeAnimateMenuView(@FabAlignmentMode int targetMode, boolean newFabAttached) {
667704
if (!ViewCompat.isLaidOut(this)) {
668705
menuAnimatingWithFabAlignmentMode = false;

lib/java/com/google/android/material/bottomappbar/res/values/attrs.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
<declare-styleable name="BottomAppBar">
1919
<!-- Background for the BottomAppBar. -->
2020
<attr name="backgroundTint"/>
21+
<!-- The tint color for the navigation button icon drawable corresponding to
22+
the "app:navigationIcon" attribute. -->
23+
<attr name="navigationIconTint"/>
2124
<!-- Elevation for the BottomAppBar. -->
2225
<attr name="elevation"/>
2326
<!-- The alignment of the fab relative to the BottomAppBar. -->

lib/java/com/google/android/material/resources/res-public/values/public.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<public name="strokeWidth" type="attr" />
2828
<public name="ensureMinTouchTargetSize" type="attr" />
2929
<public name="trackColor" type="attr" />
30+
<public name="navigationIconTint" type="attr" />
3031

3132
<!-- Navigation and datepicker -->
3233
<public name="headerLayout" type="attr"/>

lib/java/com/google/android/material/resources/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<attr name="strokeWidth" format="dimension" />
2626
<attr name="ensureMinTouchTargetSize" format="boolean" />
2727
<attr name="trackColor" format="color" />
28+
<attr name="navigationIconTint" format="color"/>
2829

2930
<!-- Navigation and Pickers -->
3031
<attr name="headerLayout" format="reference"/>

0 commit comments

Comments
 (0)