Skip to content

Commit

Permalink
Support elevation overlays for BottomNavigationView.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 238729801
  • Loading branch information
afohrman authored and cketcham committed Mar 22, 2019
1 parent ca07e5f commit d6cb349
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,14 +177,15 @@ public BottomNavigationView(Context context, AttributeSet attrs, int defStyleAtt
setItemTextColor(a.getColorStateList(R.styleable.BottomNavigationView_itemTextColor));
}

if (getBackground() == null) {
// Add a MaterialShapeDrawable as background that supports tinting in every API level.
ViewCompat.setBackground(this, createMaterialShapeDrawableBackground(context));
}

if (a.hasValue(R.styleable.BottomNavigationView_elevation)) {
ViewCompat.setElevation(
this, a.getDimensionPixelSize(R.styleable.BottomNavigationView_elevation, 0));
}
// Add a drawable as background that supports tinting in every API level.
if (getBackground() == null) {
ViewCompat.setBackground(this, new MaterialShapeDrawable());
}

ColorStateList backgroundTint =
MaterialResources.getColorStateList(
Expand Down Expand Up @@ -234,6 +235,26 @@ public void onMenuModeChange(MenuBuilder menu) {}
});
}

private MaterialShapeDrawable createMaterialShapeDrawableBackground(Context context) {
MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
materialShapeDrawable.initializeElevationOverlay(context);
return materialShapeDrawable;
}

/**
* Sets the base elevation of this view, in pixels.
*
* @attr ref android.R.styleable#View_elevation
*/
@Override
public void setElevation(float elevation) {
super.setElevation(elevation);
Drawable background = getBackground();
if (background instanceof MaterialShapeDrawable) {
((MaterialShapeDrawable) background).setElevation(elevation);
}
}

/**
* Set a listener that will be notified when a bottom navigation item is selected. This listener
* will also be notified when the currently selected item is reselected, unless an {@link
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
Use the default style to get the default bottom navigation bar with a colorSurface background and
colorPrimary for the icon and text colors. -->
<style name="Widget.MaterialComponents.BottomNavigationView" parent="Widget.Design.BottomNavigationView">
<item name="android:background">?attr/colorSurface</item>
<item name="enforceTextAppearance">true</item>
<item name="enforceMaterialTheme">true</item>
<item name="android:background">?attr/colorSurface</item>
<item name="itemBackground">@null</item>
<item name="itemHorizontalTranslationEnabled">false</item>
<item name="itemIconTint">@color/mtrl_bottom_nav_item_tint</item>
Expand All @@ -46,9 +46,9 @@
Use the colored style to get a bottom navigation bar with a colorPrimary background and
percentages of colorOnPrimary for the icon and text colors. -->
<style name="Widget.MaterialComponents.BottomNavigationView.Colored">
<item name="android:background">?attr/colorPrimary</item>
<item name="enforceTextAppearance">true</item>
<item name="enforceMaterialTheme">true</item>
<item name="android:background">?attr/colorPrimary</item>
<item name="itemIconTint">@color/mtrl_bottom_nav_colored_item_tint</item>
<item name="itemRippleColor">@color/mtrl_bottom_nav_colored_ripple_color</item>
<item name="itemTextAppearanceInactive">?attr/textAppearanceCaption</item>
Expand Down

0 comments on commit d6cb349

Please sign in to comment.