Skip to content

Commit

Permalink
[NavigationRailView] Update inset handling for Navigation Rail & Bott…
Browse files Browse the repository at this point in the history
…om Nav

Resolves #2253

GIT_ORIGIN_REV_ID=b6c2270b8855e38231c29cbfd48949afd785258c
PiperOrigin-RevId: 383632982
  • Loading branch information
ianhanniballake authored and veganafro committed Jul 8, 2021
1 parent 4044183 commit 501ef8e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import android.content.Context;
import android.os.Build.VERSION;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.appcompat.widget.TintTypedArray;
import android.util.AttributeSet;
import android.view.View;
Expand All @@ -35,6 +37,8 @@
import androidx.core.content.ContextCompat;
import com.google.android.material.behavior.HideBottomViewOnScrollBehavior;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.internal.ViewUtils.RelativePadding;
import com.google.android.material.navigation.NavigationBarMenuView;
import com.google.android.material.navigation.NavigationBarView;
import com.google.android.material.shape.MaterialShapeDrawable;
Expand Down Expand Up @@ -125,6 +129,33 @@ public BottomNavigationView(
if (shouldDrawCompatibilityTopDivider()) {
addCompatibilityTopDivider(context);
}

applyWindowInsets();
}

private void applyWindowInsets() {
ViewUtils.doOnApplyWindowInsets(
this,
new ViewUtils.OnApplyWindowInsetsListener() {
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(
View view,
@NonNull WindowInsetsCompat insets,
@NonNull RelativePadding initialPadding) {
// Apply the bottom, start, and end padding for a BottomNavigationView
// to dodge the system navigation bar
initialPadding.bottom += insets.getSystemWindowInsetBottom();

boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
int systemWindowInsetRight = insets.getSystemWindowInsetRight();
initialPadding.start += isRtl ? systemWindowInsetRight : systemWindowInsetLeft;
initialPadding.end += isRtl ? systemWindowInsetLeft : systemWindowInsetRight;
initialPadding.applyToView(view);
return insets;
}
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.AttrRes;
import androidx.annotation.DimenRes;
Expand All @@ -60,8 +59,6 @@
import androidx.customview.view.AbsSavedState;
import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.internal.ViewUtils.RelativePadding;
import com.google.android.material.resources.MaterialResources;
import com.google.android.material.ripple.RippleUtils;
import com.google.android.material.shape.MaterialShapeDrawable;
Expand Down Expand Up @@ -242,7 +239,8 @@ public NavigationBarView(
if (activeIndicatorStyleResId != 0) {
setItemActiveIndicatorEnabled(true);

@SuppressLint("CustomViewStyleable") TypedArray activeIndicatorAttributes =
@SuppressLint("CustomViewStyleable")
TypedArray activeIndicatorAttributes =
context.obtainStyledAttributes(
activeIndicatorStyleResId, R.styleable.NavigationBarActiveIndicator);

Expand Down Expand Up @@ -300,32 +298,6 @@ public boolean onMenuItemSelected(MenuBuilder menu, @NonNull MenuItem item) {
@Override
public void onMenuModeChange(MenuBuilder menu) {}
});

applyWindowInsets();
}

private void applyWindowInsets() {
ViewUtils.doOnApplyWindowInsets(
this,
new ViewUtils.OnApplyWindowInsetsListener() {
@NonNull
@Override
public androidx.core.view.WindowInsetsCompat onApplyWindowInsets(
View view,
@NonNull androidx.core.view.WindowInsetsCompat insets,
@NonNull RelativePadding initialPadding) {
// Window insets may add additional padding, e.g., to dodge the system navigation bar
initialPadding.bottom += insets.getSystemWindowInsetBottom();

boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
int systemWindowInsetRight = insets.getSystemWindowInsetRight();
initialPadding.start += isRtl ? systemWindowInsetRight : systemWindowInsetLeft;
initialPadding.end += isRtl ? systemWindowInsetLeft : systemWindowInsetRight;
initialPadding.applyToView(view);
return insets;
}
});
}

@NonNull
Expand Down Expand Up @@ -701,7 +673,7 @@ public int getItemActiveIndicatorMarginHorizontal() {
*
* @param horizontalMargin The horizontal margin, in pixels.
*/
public void setItemActiveIndicatorMarginHorizontal(@Px int horizontalMargin) {
public void setItemActiveIndicatorMarginHorizontal(@Px int horizontalMargin) {
menuView.setItemActiveIndicatorMarginHorizontal(horizontalMargin);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

import android.content.Context;
import android.content.res.Resources;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.appcompat.widget.TintTypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
Expand All @@ -35,6 +37,8 @@
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import com.google.android.material.internal.ThemeEnforcement;
import com.google.android.material.internal.ViewUtils;
import com.google.android.material.internal.ViewUtils.RelativePadding;
import com.google.android.material.navigation.NavigationBarView;

/**
Expand All @@ -50,9 +54,9 @@
* be used for programmatically selecting which destination is currently active. It can be done
* using {@code MenuItem#setChecked(true)}.
*
* <p>A header view (such as a
* {@link com.google.android.material.floatingactionbutton.FloatingActionButton}, logo, etc.) can be
* added with the {@code app:headerLayout} attribute or by using {@link #addHeaderView}.
* <p>A header view (such as a {@link
* com.google.android.material.floatingactionbutton.FloatingActionButton}, logo, etc.) can be added
* with the {@code app:headerLayout} attribute or by using {@link #addHeaderView}.
*
* <pre>
* layout resource file:
Expand Down Expand Up @@ -134,6 +138,33 @@ public NavigationRailView(
setMenuGravity(
attributes.getInt(R.styleable.NavigationRailView_menuGravity, DEFAULT_MENU_GRAVITY));
attributes.recycle();

applyWindowInsets();
}

private void applyWindowInsets() {
ViewUtils.doOnApplyWindowInsets(
this,
new ViewUtils.OnApplyWindowInsetsListener() {
@NonNull
@Override
public WindowInsetsCompat onApplyWindowInsets(
View view,
@NonNull WindowInsetsCompat insets,
@NonNull RelativePadding initialPadding) {
// Apply the top, bottom, and start padding for a start edge aligned
// NavigationRailView to dodge the system status and navigation bars
initialPadding.top += insets.getSystemWindowInsetTop();
initialPadding.bottom += insets.getSystemWindowInsetBottom();

boolean isRtl = ViewCompat.getLayoutDirection(view) == ViewCompat.LAYOUT_DIRECTION_RTL;
int systemWindowInsetLeft = insets.getSystemWindowInsetLeft();
int systemWindowInsetRight = insets.getSystemWindowInsetRight();
initialPadding.start += isRtl ? systemWindowInsetRight : systemWindowInsetLeft;
initialPadding.applyToView(view);
return insets;
}
});
}

@Override
Expand Down

0 comments on commit 501ef8e

Please sign in to comment.