Java Code Examples for androidx.core.view.ViewCompat#setOnApplyWindowInsetsListener()

The following examples show how to use androidx.core.view.ViewCompat#setOnApplyWindowInsetsListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpaqueStatusBarRelativeLayout.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
public OpaqueStatusBarRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    TypedArray ta = context.obtainStyledAttributes(
            attrs, R.styleable.OpaqueStatusBarRelativeLayout, defStyleAttr, 0);
    mInsetDrawable =
            ta.getDrawable(R.styleable.OpaqueStatusBarRelativeLayout_colorPrimaryDark);
    ta.recycle();

    setWillNotDraw(false);

    ViewCompat.setOnApplyWindowInsetsListener(this, (View v, WindowInsetsCompat insets) -> {
        mTopInset = insets.getSystemWindowInsetTop();
        setPadding(0, mTopInset, 0, 0);
        ViewCompat.postInvalidateOnAnimation(this);
        return insets.consumeSystemWindowInsets();
    });
    setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
 
Example 2
Source File: DynamicAppBarLayout.java    From dynamic-support with Apache License 2.0 6 votes vote down vote up
@Override
public void applyWindowInsets() {
    final int paddingTop = getPaddingTop();
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();

    ViewCompat.setOnApplyWindowInsetsListener(this,
            new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(paddingLeft + insets.getSystemWindowInsetLeft(),
                    paddingTop + insets.getSystemWindowInsetTop(),
                    paddingRight + insets.getSystemWindowInsetRight(),
                    v.getPaddingBottom());

            return insets;
        }
    });
}
 
Example 3
Source File: FontMainDemoFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public View onCreateDemoView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  View view =
      layoutInflater.inflate(
          R.layout.cat_font_styles_fragment, viewGroup, false /* attachToRoot */);

  RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
  recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  recyclerView.addItemDecoration(
      new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
  recyclerView.setAdapter(new FontStyleAdapter(getContext()));
  ViewCompat.setOnApplyWindowInsetsListener(recyclerView, (view1, windowInsetsCompat) -> {
    recyclerView.setClipToPadding(windowInsetsCompat.getSystemWindowInsetBottom() == 0);
    recyclerView.setPadding(
        recyclerView.getPaddingLeft(),
        recyclerView.getPaddingTop(),
        recyclerView.getPaddingRight(),
        windowInsetsCompat.getSystemWindowInsetBottom());

    return windowInsetsCompat;
  });

  return view;
}
 
Example 4
Source File: DynamicBottomSheet.java    From dynamic-support with Apache License 2.0 6 votes vote down vote up
@Override
public void applyWindowInsets() {
    if (getParent() == null || !(getParent() instanceof CoordinatorLayout)) {
        return;
    }

    final int paddingBottom = getPaddingBottom();
    final int peekHeight = BottomSheetBehavior.from(this).getPeekHeight();
    ViewCompat.setOnApplyWindowInsetsListener(this,
            new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(),
                    paddingBottom + insets.getSystemWindowInsetBottom());
            BottomSheetBehavior.from(v).setPeekHeight(
                    peekHeight + insets.getSystemWindowInsetBottom());
            insets.consumeSystemWindowInsets();

            return insets;
        }
    });
}
 
Example 5
Source File: DynamicViewUtils.java    From dynamic-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Apply window insets padding for the supplied view.
 *
 * @param view The view to set the insets padding.
 * @param left {@code true} to apply the left window inset padding.
 * @param top {@code true} to apply the top window inset padding.
 * @param right {@code true} to apply the right window inset padding.
 * @param bottom {@code true} to apply the bottom window inset padding.
 * @param consume {@code true} to consume the applied window insets.
 */
public static void applyWindowInsets(@Nullable View view, final boolean left,
        final boolean top, final boolean right, final boolean bottom, final boolean consume) {
    if (view == null) {
        return;
    }

    final int paddingLeft = view.getPaddingLeft();
    final int paddingTop = view.getPaddingTop();
    final int paddingRight = view.getPaddingRight();
    final int paddingBottom = view.getPaddingBottom();

    ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(left ? paddingLeft + insets.getSystemWindowInsetLeft(): paddingLeft,
                    top ? paddingTop + insets.getSystemWindowInsetTop() : paddingTop,
                    right ? paddingRight + insets.getSystemWindowInsetRight() : paddingRight,
                    bottom ? paddingBottom + insets.getSystemWindowInsetBottom() : paddingBottom);

            return !consume ? insets :
                    new WindowInsetsCompat.Builder(insets).setSystemWindowInsets(
                            Insets.of(left ? 0 : insets.getSystemWindowInsetLeft(),
                                    top ? 0 : insets.getSystemWindowInsetTop(),
                                    right ? 0 : insets.getSystemWindowInsetRight(),
                                    bottom ? 0 : insets.getSystemWindowInsetBottom()))
                            .build();
        }
    });

    requestApplyWindowInsets(view);
}
 
Example 6
Source File: ScrimInsetsRelativeLayout.java    From HaoReader with GNU General Public License v3.0 5 votes vote down vote up
private void init(Context context, AttributeSet attrs, int defStyle) {
    final TypedArray a = context.obtainStyledAttributes(attrs,
            R.styleable.ScrimInsetsRelativeLayout, defStyle, 0);
    mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsRelativeLayout_appInsetForeground);
    mConsumeInsets = a.getBoolean(R.styleable.ScrimInsetsRelativeLayout_appConsumeInsets, true);
    mFitTop = a.getBoolean(R.styleable.ScrimInsetsRelativeLayout_fitTop, true);
    mFitBottom = a.getBoolean(R.styleable.ScrimInsetsRelativeLayout_fitBottom, false);
    mFitLeft = a.getBoolean(R.styleable.ScrimInsetsRelativeLayout_fitLeft, false);
    mFitRight = a.getBoolean(R.styleable.ScrimInsetsRelativeLayout_fitRight, false);
    a.recycle();

    setWillNotDraw(true);
    ViewCompat.setOnApplyWindowInsetsListener(this, (v, insets) -> {
        if (!mConsumeInsets) {
            if (mOnInsetsCallback != null) {
                mOnInsetsCallback.onInsetsChanged(new Rect(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom()));
            }
            return insets.consumeSystemWindowInsets();
        }

        if (null == ScrimInsetsRelativeLayout.this.mInsets) {
            ScrimInsetsRelativeLayout.this.mInsets = new Rect();
        }

        ScrimInsetsRelativeLayout.this.mInsets.set(insets.getSystemWindowInsetLeft(), insets.getSystemWindowInsetTop(), insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
        ScrimInsetsRelativeLayout.this.onInsetsChanged(ScrimInsetsRelativeLayout.this.mInsets);
        ScrimInsetsRelativeLayout.this.setWillNotDraw(!insets.hasSystemWindowInsets() || ScrimInsetsRelativeLayout.this.mInsetForeground == null);
        ViewCompat.postInvalidateOnAnimation(ScrimInsetsRelativeLayout.this);
        return insets.consumeSystemWindowInsets();
    });
}
 
Example 7
Source File: MaterialDialogDecorator.java    From AndroidMaterialDialog with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
protected final Map<ViewType, View> onAttach(@NonNull final Window window,
                                             @NonNull final View view,
                                             @NonNull final Map<ViewType, View> areas,
                                             final Void param) {
    ViewCompat.setOnApplyWindowInsetsListener(view, createWindowInsetsListener());
    View titleView = inflateTitleView();
    View messageView = inflateMessageView();
    View contentView = inflateContentView();

    if (titleView != null && messageView != null && contentView != null) {
        adaptWindowBackgroundAndInset();
        adaptLayoutParams();
        adaptPadding();
        adaptScrollableArea();
        adaptDividerVisibility();
        adaptTitle();
        adaptTitleColor();
        adaptTitleTypeface();
        adaptIcon();
        adaptMessage();
        adaptMessageColor();
        adaptMessageTypeface();
        adaptBackground(null);
        Map<ViewType, View> result = new HashMap<>();
        result.put(new AreaViewType(Area.TITLE), titleContainer);
        result.put(new AreaViewType(Area.MESSAGE), messageContainer);
        result.put(new AreaViewType(Area.CONTENT), contentContainer);
        return result;
    }

    return Collections.emptyMap();
}
 
Example 8
Source File: AbstractActivity.java    From ground-android with Apache License 2.0 5 votes vote down vote up
@Override
public void setContentView(View view) {
  super.setContentView(view);
  ViewCompat.setOnApplyWindowInsetsListener(
      getWindow().getDecorView().getRootView(),
      (v, insets) -> {
        onWindowInsetChanged(insets);
        return insets;
      });
}
 
Example 9
Source File: ChildController.java    From react-native-navigation with MIT License 5 votes vote down vote up
@Override
public T getView() {
    if (view == null) {
        super.getView();
        view.setFitsSystemWindows(true);
        ViewCompat.setOnApplyWindowInsetsListener(view, this::onApplyWindowInsets);
    }
    return view;
}
 
Example 10
Source File: MediaPreviewActivity.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static void anchorMarginsToBottomInsets(@NonNull View viewToAnchor) {
  ViewCompat.setOnApplyWindowInsetsListener(viewToAnchor, (view, insets) -> {
    ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) view.getLayoutParams();

    layoutParams.setMargins(insets.getSystemWindowInsetLeft(),
                            layoutParams.topMargin,
                            insets.getSystemWindowInsetRight(),
                            insets.getSystemWindowInsetBottom());

    view.setLayoutParams(layoutParams);

    return insets;
  });
}
 
Example 11
Source File: BottomNavigationAnimatedDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public View onCreateView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  View view = super.onCreateView(layoutInflater, viewGroup, bundle);
  CoordinatorLayout coordinatorLayout = view.findViewById(R.id.cat_demo_fragment_container);
  // For unknown reasons, setting this in the xml is cleared out but setting it here takes effect.
  CoordinatorLayout.LayoutParams lp =
      (LayoutParams) coordinatorLayout.getChildAt(0).getLayoutParams();
  lp.gravity = Gravity.BOTTOM;
  ViewCompat.setOnApplyWindowInsetsListener(coordinatorLayout, (v, insets) -> insets);
  return view;
}
 
Example 12
Source File: FitBottomSystemBarNestedScrollView.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FitBottomSystemBarNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
 
Example 13
Source File: DemoUtils.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public static void addBottomSpaceInsetsIfNeeded(
    ViewGroup scrollableViewAncestor, ViewGroup demoContainer) {
  List<? extends ViewGroup> scrollViews =
      DemoUtils.findViewsWithType(scrollableViewAncestor, ScrollView.class);

  List<? extends ViewGroup> nestedScrollViews = DemoUtils
      .findViewsWithType(scrollableViewAncestor, NestedScrollView.class);

  ArrayList<ViewGroup> scrollingViews = new ArrayList<>();
  scrollingViews.addAll(scrollViews);
  scrollingViews.addAll(nestedScrollViews);
  ViewCompat.setOnApplyWindowInsetsListener(
      demoContainer,
      (view, insets) -> {
        for (ViewGroup scrollView : scrollingViews) {
          scrollView.addOnLayoutChangeListener(
              new OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(
                    View v,
                    int left,
                    int top,
                    int right,
                    int bottom,
                    int oldLeft,
                    int oldTop,
                    int oldRight,
                    int oldBottom) {
                  scrollView.removeOnLayoutChangeListener(this);
                  int systemWindowInsetBottom = insets.getSystemWindowInsetBottom();
                  if (!shouldApplyBottomInset(scrollView, systemWindowInsetBottom)) {
                    return;
                  }

                  int insetBottom = calculateBottomInset(scrollView, systemWindowInsetBottom);
                  View scrollableContent = scrollView.getChildAt(0);
                  scrollableContent.setPadding(
                      scrollableContent.getPaddingLeft(),
                      scrollableContent.getPaddingTop(),
                      scrollableContent.getPaddingRight(),
                      insetBottom);
                }
              });
        }
        return insets;
      });
}
 
Example 14
Source File: CollapsingToolbarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public CollapsingToolbarLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(wrap(context, attrs, defStyleAttr, DEF_STYLE_RES), attrs, defStyleAttr);
  // Ensure we are using the correctly themed context rather than the context that was passed in.
  context = getContext();

  collapsingTextHelper = new CollapsingTextHelper(this);
  collapsingTextHelper.setTextSizeInterpolator(AnimationUtils.DECELERATE_INTERPOLATOR);

  TypedArray a =
      ThemeEnforcement.obtainStyledAttributes(
          context,
          attrs,
          R.styleable.CollapsingToolbarLayout,
          defStyleAttr,
          DEF_STYLE_RES);

  collapsingTextHelper.setExpandedTextGravity(
      a.getInt(
          R.styleable.CollapsingToolbarLayout_expandedTitleGravity,
          GravityCompat.START | Gravity.BOTTOM));
  collapsingTextHelper.setCollapsedTextGravity(
      a.getInt(
          R.styleable.CollapsingToolbarLayout_collapsedTitleGravity,
          GravityCompat.START | Gravity.CENTER_VERTICAL));

  expandedMarginStart =
      expandedMarginTop =
          expandedMarginEnd =
              expandedMarginBottom =
                  a.getDimensionPixelSize(
                      R.styleable.CollapsingToolbarLayout_expandedTitleMargin, 0);

  if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleMarginStart)) {
    expandedMarginStart =
        a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_expandedTitleMarginStart, 0);
  }
  if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleMarginEnd)) {
    expandedMarginEnd =
        a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_expandedTitleMarginEnd, 0);
  }
  if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleMarginTop)) {
    expandedMarginTop =
        a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_expandedTitleMarginTop, 0);
  }
  if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleMarginBottom)) {
    expandedMarginBottom =
        a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_expandedTitleMarginBottom, 0);
  }

  collapsingTitleEnabled = a.getBoolean(R.styleable.CollapsingToolbarLayout_titleEnabled, true);
  setTitle(a.getText(R.styleable.CollapsingToolbarLayout_title));

  // First load the default text appearances
  collapsingTextHelper.setExpandedTextAppearance(
      R.style.TextAppearance_Design_CollapsingToolbar_Expanded);
  collapsingTextHelper.setCollapsedTextAppearance(
      androidx.appcompat.R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);

  // Now overlay any custom text appearances
  if (a.hasValue(R.styleable.CollapsingToolbarLayout_expandedTitleTextAppearance)) {
    collapsingTextHelper.setExpandedTextAppearance(
        a.getResourceId(R.styleable.CollapsingToolbarLayout_expandedTitleTextAppearance, 0));
  }
  if (a.hasValue(R.styleable.CollapsingToolbarLayout_collapsedTitleTextAppearance)) {
    collapsingTextHelper.setCollapsedTextAppearance(
        a.getResourceId(R.styleable.CollapsingToolbarLayout_collapsedTitleTextAppearance, 0));
  }

  scrimVisibleHeightTrigger =
      a.getDimensionPixelSize(R.styleable.CollapsingToolbarLayout_scrimVisibleHeightTrigger, -1);

  if (a.hasValue(R.styleable.CollapsingToolbarLayout_maxLines)) {
    collapsingTextHelper.setMaxLines(a.getInt(R.styleable.CollapsingToolbarLayout_maxLines, 1));
  }

  scrimAnimationDuration =
      a.getInt(
          R.styleable.CollapsingToolbarLayout_scrimAnimationDuration,
          DEFAULT_SCRIM_ANIMATION_DURATION);

  setContentScrim(a.getDrawable(R.styleable.CollapsingToolbarLayout_contentScrim));
  setStatusBarScrim(a.getDrawable(R.styleable.CollapsingToolbarLayout_statusBarScrim));

  toolbarId = a.getResourceId(R.styleable.CollapsingToolbarLayout_toolbarId, -1);

  a.recycle();

  setWillNotDraw(false);

  ViewCompat.setOnApplyWindowInsetsListener(
      this,
      new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(
            View v, @NonNull WindowInsetsCompat insets) {
          return onWindowInsetChanged(insets);
        }
      });
}
 
Example 15
Source File: TocFragment.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public View onCreateView(
    LayoutInflater layoutInflater, @Nullable ViewGroup viewGroup, @Nullable Bundle bundle) {
  View view =
      layoutInflater.inflate(R.layout.cat_toc_fragment, viewGroup, false /* attachToRoot */);

  Toolbar toolbar = view.findViewById(R.id.toolbar);
  AppCompatActivity activity = (AppCompatActivity) getActivity();
  activity.setSupportActionBar(toolbar);
  activity.getSupportActionBar().setDisplayShowTitleEnabled(false);

  ViewGroup content = view.findViewById(R.id.content);
  View.inflate(getContext(), tocResourceProvider.getHeaderContent(), content);

  appBarLayout = view.findViewById(R.id.cat_toc_app_bar_layout);
  gridTopDivider = view.findViewById(R.id.cat_toc_grid_top_divider);
  recyclerView = view.findViewById(R.id.cat_toc_grid);
  themeButton = view.findViewById(R.id.cat_toc_theme_button);
  edgeToEdgeButton = view.findViewById(R.id.cat_edge_to_edge_button);

  ViewCompat.setOnApplyWindowInsetsListener(
      view,
      (v, insetsCompat) -> {
        appBarLayout
            .findViewById(R.id.cat_toc_collapsingtoolbarlayout)
            .setPadding(0, insetsCompat.getSystemWindowInsetTop(), 0, 0);
        return insetsCompat;
      });

  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
    addGridTopDividerVisibilityListener();
  } else {
    gridTopDivider.setVisibility(View.VISIBLE);
  }

  final int gridSpanCount = calculateGridSpanCount();

  recyclerView.setLayoutManager(new GridLayoutManager(getContext(), gridSpanCount));
  recyclerView.addItemDecoration(
      new GridDividerDecoration(
          getResources().getDimensionPixelSize(R.dimen.cat_toc_grid_divider_size),
          ContextCompat.getColor(getContext(), R.color.cat_toc_grid_divider_color),
          gridSpanCount));

  List<FeatureDemo> featureList = new ArrayList<>(featureDemos);
  // Sort features alphabetically
  Collator collator = Collator.getInstance();
  Collections.sort(
      featureList,
      (feature1, feature2) ->
          collator.compare(
              getContext().getString(feature1.getTitleResId()),
              getContext().getString(feature2.getTitleResId())));

  TocAdapter tocAdapter =
      new TocAdapter(getActivity(), featureList, containerTransformConfiguration);
  recyclerView.setAdapter(tocAdapter);

  initThemeButton();

  initEdgeToEdgeButton();
  return view;
}
 
Example 16
Source File: FitBottomSystemBarRecyclerView.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FitBottomSystemBarRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
 
Example 17
Source File: FitBottomSystemBarNestedScrollView.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FitBottomSystemBarNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
 
Example 18
Source File: FitBottomSystemBarNestedScrollView.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FitBottomSystemBarNestedScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    ViewCompat.setOnApplyWindowInsetsListener(this, null);
}
 
Example 19
Source File: DynamicViewUtils.java    From dynamic-utils with Apache License 2.0 4 votes vote down vote up
/**
 * Apply window insets margin for the supplied view.
 *
 * @param view The view to set the insets margin.
 * @param left {@code true} to apply the left window inset margin.
 * @param top {@code true} to apply the top window inset margin.
 * @param right {@code true} to apply the right window inset margin.
 * @param bottom {@code true} to apply the bottom window inset margin.
 * @param consume {@code true} to consume the applied window margin.
 */
public static void applyWindowInsetsMargin(@Nullable View view, final boolean left,
        final boolean top, final boolean right, final boolean bottom, final boolean consume) {
    if (view == null) {
        return;
    }

    if (view.getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
        final ViewGroup.MarginLayoutParams layoutParams =
                (ViewGroup.MarginLayoutParams) view.getLayoutParams();

        final int marginLeft = layoutParams.leftMargin;
        final int marginTop = layoutParams.topMargin;
        final int marginRight = layoutParams.rightMargin;
        final int marginBottom = layoutParams.bottomMargin;

        ViewCompat.setOnApplyWindowInsetsListener(view, new OnApplyWindowInsetsListener() {
            @Override
            public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
                if (left) {
                    layoutParams.leftMargin = marginLeft
                            + insets.getSystemWindowInsetLeft();
                }
                if (top) {
                    layoutParams.topMargin = marginTop
                            + insets.getSystemWindowInsetTop();
                }
                if (right) {
                    layoutParams.rightMargin = marginRight
                            + insets.getSystemWindowInsetRight();
                }
                if (bottom) {
                    layoutParams.bottomMargin = marginBottom
                            + insets.getSystemWindowInsetBottom();
                }

                return !consume ? insets :
                        new WindowInsetsCompat.Builder(insets).setSystemWindowInsets(
                                Insets.of(left ? 0 : insets.getSystemWindowInsetLeft(),
                                        top ? 0 : insets.getSystemWindowInsetTop(),
                                        right ? 0 : insets.getSystemWindowInsetRight(),
                                        bottom ? 0 : insets.getSystemWindowInsetBottom()))
                                .build();
            }
        });
    }
}
 
Example 20
Source File: FitBottomSystemBarNestedScrollView.java    From GeometricWeather with GNU Lesser General Public License v3.0 4 votes vote down vote up
public FitBottomSystemBarNestedScrollView(@NonNull Context context) {
    super(context);
    ViewCompat.setOnApplyWindowInsetsListener(this, null);
}