Skip to content

Commit 1795035

Browse files
committed
[NavigationView] Add start/end scrim enable/disable methods
PiperOrigin-RevId: 666430643
1 parent 29ce8d3 commit 1795035

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

docs/components/NavigationDrawer.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,10 @@ Element | Attribute | Related
322322

323323
### Scrim attributes
324324

325-
Element | Attribute | Related method(s) | Default value
326-
--------- | --------- | --------------------------------- | --------------------
327-
**Color** | N/A | `setScrimColor` on `DrawerLayout` | Black at 60% opacity
325+
Element | Attribute | Related method(s) | Default value
326+
----------------- | ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------
327+
**Color** | N/A | `setScrimColor` on `DrawerLayout` | Black at 60% opacity
328+
**Window Insets** | `app:topInsetScrimEnabled`<br/>`app:bottomScrimEnabled`<br/>`app:startScrimEnabled`<br/>`app:endScrimEnabled` | `setTopInsetScrimEnabled`<br/>`isTopInsetScrimEnabled`<br/>`setBottomInsetScrimEnabled`<br/>`isBottomInsetScrimEnabled`<br/>`setStartInsetScrimEnabled`<br/>`isStartInsetScrimEnabled`<br/>`setEndInsetScrimEnabled`<br/>`isEndInsetScrimEnabled` | true
328329

329330
### `NavigationView` styles
330331

lib/java/com/google/android/material/navigation/NavigationView.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ public class NavigationView extends ScrimInsetsFrameLayout implements MaterialBa
142142
private OnGlobalLayoutListener onGlobalLayoutListener;
143143
private boolean topInsetScrimEnabled = true;
144144
private boolean bottomInsetScrimEnabled = true;
145+
private boolean startInsetScrimEnabled = true;
146+
private boolean endInsetScrimEnabled = true;
145147

146148
@Px private int drawerLayoutCornerSize = 0;
147149
private final boolean drawerLayoutCornerSizeBackAnimationEnabled;
@@ -328,6 +330,11 @@ public NavigationView(@NonNull Context context, @Nullable AttributeSet attrs, in
328330

329331
setBottomInsetScrimEnabled(
330332
a.getBoolean(R.styleable.NavigationView_bottomInsetScrimEnabled, bottomInsetScrimEnabled));
333+
setStartInsetScrimEnabled(
334+
a.getBoolean(R.styleable.NavigationView_startInsetScrimEnabled, startInsetScrimEnabled));
335+
336+
setEndInsetScrimEnabled(
337+
a.getBoolean(R.styleable.NavigationView_endInsetScrimEnabled, endInsetScrimEnabled));
331338

332339
final int itemIconPadding =
333340
a.getDimensionPixelSize(R.styleable.NavigationView_itemIconPadding, 0);
@@ -933,6 +940,34 @@ public void setBottomInsetScrimEnabled(boolean enabled) {
933940
this.bottomInsetScrimEnabled = enabled;
934941
}
935942

943+
/** Whether or not the NavigationView will draw a scrim behind the window's start inset. */
944+
public boolean isStartInsetScrimEnabled() {
945+
return this.startInsetScrimEnabled;
946+
}
947+
948+
/**
949+
* Set whether or not the NavigationView should draw a scrim behind the window's start inset.
950+
*
951+
* @param enabled true when the NavigationView should draw a scrim.
952+
*/
953+
public void setStartInsetScrimEnabled(boolean enabled) {
954+
this.startInsetScrimEnabled = enabled;
955+
}
956+
957+
/** Whether or not the NavigationView will draw a scrim behind the window's end inset. */
958+
public boolean isEndInsetScrimEnabled() {
959+
return this.endInsetScrimEnabled;
960+
}
961+
962+
/**
963+
* Set whether or not the NavigationView should draw a scrim behind the window's end inset.
964+
*
965+
* @param enabled true when the NavigationView should draw a scrim.
966+
*/
967+
public void setEndInsetScrimEnabled(boolean enabled) {
968+
this.endInsetScrimEnabled = enabled;
969+
}
970+
936971
/**
937972
* Get the distance between the start edge of the NavigationView and the start of a menu divider.
938973
*/
@@ -1087,10 +1122,12 @@ public void onGlobalLayout() {
10871122
presenter.setBehindStatusBar(isBehindStatusBar);
10881123
setDrawTopInsetForeground(isBehindStatusBar && isTopInsetScrimEnabled());
10891124

1125+
boolean isRtl = getLayoutDirection() == LAYOUT_DIRECTION_RTL;
10901126
// The navigation view could be left aligned or just hidden out of view in a drawer
10911127
// layout when the global layout listener is called.
10921128
boolean isOnLeftSide = (tmpLocation[0] == 0) || (tmpLocation[0] + getWidth() == 0);
1093-
setDrawLeftInsetForeground(isOnLeftSide);
1129+
setDrawLeftInsetForeground(
1130+
isOnLeftSide && (isRtl ? isEndInsetScrimEnabled() : isStartInsetScrimEnabled()));
10941131

10951132
Activity activity = ContextUtils.getActivity(getContext());
10961133
if (activity != null && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
@@ -1108,7 +1145,8 @@ public void onGlobalLayout() {
11081145
(displayBounds.width() == tmpLocation[0])
11091146
|| (displayBounds.width() - getWidth() == tmpLocation[0]);
11101147

1111-
setDrawRightInsetForeground(isOnRightSide);
1148+
setDrawRightInsetForeground(
1149+
isOnRightSide && (isRtl ? isStartInsetScrimEnabled() : isEndInsetScrimEnabled()));
11121150
}
11131151
}
11141152
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
<public name="labelVisibilityMode" type="attr"/>
4040
<public name="topInsetScrimEnabled" type="attr"/>
4141
<public name="bottomInsetScrimEnabled" type="attr"/>
42+
<public name="startInsetScrimEnabled" type="attr"/>
43+
<public name="endInsetScrimEnabled" type="attr"/>
4244
<public name="menu" type="attr"/>
4345
<public name="subheaderColor" type="attr"/>
4446
<public name="subheaderTextAppearance" type="attr"/>

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@
199199
<!-- Whether or not the navigation view should draw a scrim behind the bottom window
200200
inset, usually the navigation area, when drawing edge to edge. -->
201201
<attr name="bottomInsetScrimEnabled" format="boolean"/>
202+
<!-- Whether or not the navigation view should draw a scrim behind the start window
203+
inset when drawing edge to edge. -->
204+
<attr name="startInsetScrimEnabled" format="boolean"/>
205+
<!-- Whether or not the navigation view should draw a scrim behind the end window
206+
inset when drawing edge to edge. -->
207+
<attr name="endInsetScrimEnabled" format="boolean"/>
202208
<!-- The distance between the start of the navigation view container and a menu divider. -->
203209
<attr name="dividerInsetStart"/>
204210
<!-- The distance between the end of the navigation view container and a menu divider. -->

0 commit comments

Comments
 (0)