androidx.core.view.WindowInsetsCompat Java Examples

The following examples show how to use androidx.core.view.WindowInsetsCompat. 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: CoordinatorScrollingFrameLayout.java    From AndroidFastScroll with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onMeasureChild(@NonNull CoordinatorLayout parent, @NonNull View child,
                              int parentWidthMeasureSpec, int widthUsed,
                              int parentHeightMeasureSpec, int heightUsed) {
    @SuppressLint("RestrictedApi")
    WindowInsetsCompat parentInsets = parent.getLastWindowInsets();
    if (parentInsets != null) {
        int parentHeightSize = MeasureSpec.getSize(parentHeightMeasureSpec);
        parentHeightSize -= parentInsets.getSystemWindowInsetTop()
                + parentInsets.getSystemWindowInsetBottom();
        int parentHeightMode = MeasureSpec.getMode(parentHeightMeasureSpec);
        parentHeightMeasureSpec = MeasureSpec.makeMeasureSpec(parentHeightSize,
                parentHeightMode);
    }
    return super.onMeasureChild(parent, child, parentWidthMeasureSpec, widthUsed,
            parentHeightMeasureSpec, heightUsed);
}
 
Example #2
Source File: WindowInsetsFrameLayout.java    From earth with GNU General Public License v3.0 6 votes vote down vote up
@TargetApi(21)
private boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
    boolean consumed = false;

    for (int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);

        if (!child.getFitsSystemWindows()) {
            continue;
        }

        Rect childInsets = new Rect(
                insets.getSystemWindowInsetLeft(),
                insets.getSystemWindowInsetTop(),
                insets.getSystemWindowInsetRight(),
                insets.getSystemWindowInsetBottom());

        computeInsetsWithGravity(child, childInsets);

        ViewCompat.dispatchApplyWindowInsets(child, insets.replaceSystemWindowInsets(childInsets));

        consumed = true;
    }

    return consumed;
}
 
Example #3
Source File: MaterialDialogDecorator.java    From AndroidMaterialDialog with Apache License 2.0 6 votes vote down vote up
/**
 * Creates and returns a listener, which allows to observe when window insets are applied to the
 * root view of the view hierarchy, which is modified by the decorator.
 *
 * @return The listener, which has been created, as an instance of the type {@link
 * OnApplyWindowInsetsListener}
 */
private OnApplyWindowInsetsListener createWindowInsetsListener() {
    return new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                      final WindowInsetsCompat insets) {
            systemWindowInsets = insets.hasSystemWindowInsets() ?
                    new Rect(insets.getSystemWindowInsetLeft(),
                            insets.getSystemWindowInsetTop(),
                            insets.getSystemWindowInsetRight(),
                            insets.getSystemWindowInsetBottom()) : null;
            adaptLayoutParams();
            return insets;
        }

    };
}
 
Example #4
Source File: CollapsingToolbarLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
WindowInsetsCompat onWindowInsetChanged(@NonNull final WindowInsetsCompat insets) {
  WindowInsetsCompat newInsets = null;

  if (ViewCompat.getFitsSystemWindows(this)) {
    // If we're set to fit system windows, keep the insets
    newInsets = insets;
  }

  // If our insets have changed, keep them and invalidate the scroll ranges...
  if (!ObjectsCompat.equals(lastInsets, newInsets)) {
    lastInsets = newInsets;
    requestLayout();
  }

  // Consume the insets. This is done so that child views with fitSystemWindows=true do not
  // get the default padding functionality from View
  return insets.consumeSystemWindowInsets();
}
 
Example #5
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
WindowInsetsCompat onWindowInsetChanged(final WindowInsetsCompat insets) {
  WindowInsetsCompat newInsets = null;

  if (ViewCompat.getFitsSystemWindows(this)) {
    // If we're set to fit system windows, keep the insets
    newInsets = insets;
  }

  // If our insets have changed, keep them and trigger a layout...
  if (!ObjectsCompat.equals(lastInsets, newInsets)) {
    lastInsets = newInsets;
    updateWillNotDraw();
    requestLayout();
  }

  return insets;
}
 
Example #6
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
WindowInsetsCompat onWindowInsetChanged(final WindowInsetsCompat insets) {
    WindowInsetsCompat newInsets = null;

    if (ViewCompat.getFitsSystemWindows(this)) {
        // If we're set to fit system windows, keep the insets
        newInsets = insets;
    }

    // If our insets have changed, keep them and invalidate the scroll ranges...
    if (!objectEquals(mLastInsets, newInsets)) {
        mLastInsets = newInsets;
        requestLayout();
    }

    // Consume the insets. This is done so that child views with fitSystemWindows=true do not
    // get the default padding functionality from View
    return insets.consumeSystemWindowInsets();
}
 
Example #7
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 #8
Source File: SubtitleCollapsingToolbarLayout.java    From collapsingtoolbarlayout-subtitle with Apache License 2.0 6 votes vote down vote up
WindowInsetsCompat onWindowInsetChanged(@NonNull final WindowInsetsCompat insets) {
    WindowInsetsCompat newInsets = null;

    if (ViewCompat.getFitsSystemWindows(this)) {
        // If we're set to fit system windows, keep the insets
        newInsets = insets;
    }

    // If our insets have changed, keep them and invalidate the scroll ranges...
    if (!ObjectsCompat.equals(lastInsets, newInsets)) {
        lastInsets = newInsets;
        requestLayout();
    }

    // Consume the insets. This is done so that child views with fitSystemWindows=true do not
    // get the default padding functionality from View
    return insets.consumeSystemWindowInsets();
}
 
Example #9
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 #10
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 #11
Source File: GoogleMapsFragment.java    From ground-android with Apache License 2.0 5 votes vote down vote up
private WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) {
  int insetBottom = insets.getSystemWindowInsetBottom();
  // TODO: Move extra padding to dimens.xml.
  // HACK: Fix padding when keyboard is shown; we limit the padding here to prevent the
  // watermark from flying up too high due to the combination of translateY and big inset
  // size due to keyboard.
  setWatermarkPadding(view, 20, 0, 0, Math.min(insetBottom, 250) + 8);
  return insets;
}
 
Example #12
Source File: InsetsPercentRelativeLayout.java    From Bop with Apache License 2.0 5 votes vote down vote up
public InsetsPercentRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    ViewCompat.setOnApplyWindowInsetsListener(this, new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            setWindowInsets(insets);
            return insets.consumeSystemWindowInsets();
        }
    });
}
 
Example #13
Source File: ScrimInsetsFrameLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public ScrimInsetsFrameLayout(
    @NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);

  final TypedArray a =
      ThemeEnforcement.obtainStyledAttributes(
          context,
          attrs,
          R.styleable.ScrimInsetsFrameLayout,
          defStyleAttr,
          R.style.Widget_Design_ScrimInsetsFrameLayout);
  insetForeground = a.getDrawable(R.styleable.ScrimInsetsFrameLayout_insetForeground);
  a.recycle();
  setWillNotDraw(true); // No need to draw until the insets are adjusted

  ViewCompat.setOnApplyWindowInsetsListener(
      this,
      new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(
            View v, @NonNull WindowInsetsCompat insets) {
          if (null == ScrimInsetsFrameLayout.this.insets) {
            ScrimInsetsFrameLayout.this.insets = new Rect();
          }
          ScrimInsetsFrameLayout.this.insets.set(
              insets.getSystemWindowInsetLeft(),
              insets.getSystemWindowInsetTop(),
              insets.getSystemWindowInsetRight(),
              insets.getSystemWindowInsetBottom());
          onInsetsChanged(insets);
          setWillNotDraw(!insets.hasSystemWindowInsets() || insetForeground == null);
          ViewCompat.postInvalidateOnAnimation(ScrimInsetsFrameLayout.this);
          return insets.consumeSystemWindowInsets();
        }
      });
}
 
Example #14
Source File: NavigationMenuPresenter.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public void dispatchApplyWindowInsets(@NonNull WindowInsetsCompat insets) {
  int top = insets.getSystemWindowInsetTop();
  if (paddingTopDefault != top) {
    paddingTopDefault = top;
    // Apply the padding to the top of the view if it has changed.
    updateTopPadding();
  }

  // Always apply the bottom padding.
  menuView.setPadding(0, menuView.getPaddingTop(), 0, insets.getSystemWindowInsetBottom());
  ViewCompat.dispatchApplyWindowInsets(headerLayout, insets);
}
 
Example #15
Source File: InsetsPercentRelativeLayout.java    From Bop with Apache License 2.0 5 votes vote down vote up
private void setWindowInsets(WindowInsetsCompat insets) {
    // Now dispatch them to our children
    for (int i = 0, z = getChildCount(); i < z; i++) {
        final View child = getChildAt(i);
        insets = ViewCompat.dispatchApplyWindowInsets(child, insets);
        if (insets.isConsumed()) {
            break;
        }
    }
}
 
Example #16
Source File: SnackbarWithTranslucentNavBarTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
@MediumTest
public void testDrawsAboveNavigationBar() {
  // Show a simple Snackbar and wait for it to be shown
  final Snackbar snackbar = Snackbar.make(coordinatorLayout, MESSAGE_TEXT, Snackbar.LENGTH_SHORT);
  SnackbarUtils.showTransientBottomBarAndWaitUntilFullyShown(snackbar);

  final WindowInsetsCompat colLastInsets = coordinatorLayout.getLastWindowInsets();
  assertNotNull(colLastInsets);

  // Check that the Snackbar view has padding set to display above the nav bar
  final View view = snackbar.getView();
  assertNotNull(view);
  assertEquals(colLastInsets.getSystemWindowInsetBottom(), view.getPaddingBottom());
}
 
Example #17
Source File: ChildController.java    From react-native-navigation with MIT License 5 votes vote down vote up
protected WindowInsetsCompat applyWindowInsets(ViewController view, WindowInsetsCompat insets) {
    return insets.replaceSystemWindowInsets(
            insets.getSystemWindowInsetLeft(),
            0,
            insets.getSystemWindowInsetRight(),
            insets.getSystemWindowInsetBottom()
    );
}
 
Example #18
Source File: ComponentViewController.java    From react-native-navigation with MIT License 5 votes vote down vote up
@Override
protected WindowInsetsCompat applyWindowInsets(ViewController view, WindowInsetsCompat insets) {
    ViewCompat.onApplyWindowInsets(view.getView(), insets.replaceSystemWindowInsets(
            insets.getSystemWindowInsetLeft(),
            insets.getSystemWindowInsetTop(),
            insets.getSystemWindowInsetRight(),
            Math.max(insets.getSystemWindowInsetBottom() - getBottomInset(), 0)
    ));
    return insets;
}
 
Example #19
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 #20
Source File: PadNavigationView.java    From Intra with Apache License 2.0 5 votes vote down vote up
@Override
protected void onInsetsChanged(WindowInsetsCompat insets) {
  // Normally, NavigationView applies the system inset padding to the first menu item, not to the
  // NavigationView itself.  This doesn't work if the view has no MenuItems.  By overriding the
  // inset listener and applying the padding directly to the NavigationView, we can get correct
  // padding behavior even when there are no menu items.
  setPadding(getPaddingLeft(), insets.getSystemWindowInsetTop(), getPaddingRight(),
      getPaddingBottom());
}
 
Example #21
Source File: DynamicCollapsingToolbarLayout.java    From dynamic-support with Apache License 2.0 5 votes vote down vote up
@Override
public void applyWindowInsets() {
    ViewCompat.setOnApplyWindowInsetsListener(this,
            new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(v.getPaddingLeft(), v.getPaddingTop(),
                    v.getPaddingRight(), v.getPaddingBottom());

            return insets;
        }
    });
}
 
Example #22
Source File: AbstractActivity.java    From ground-android with Apache License 2.0 5 votes vote down vote up
/** Adjust UI elements with respect to top/bottom insets. */
@OverridingMethodsMustInvokeSuper
protected void onWindowInsetChanged(WindowInsetsCompat insets) {
  findViewById(R.id.status_bar_scrim)
      .setLayoutParams(
          new FrameLayout.LayoutParams(
              LayoutParams.MATCH_PARENT, insets.getSystemWindowInsetTop()));
}
 
Example #23
Source File: HomeScreenFragment.java    From ground-android with Apache License 2.0 5 votes vote down vote up
private void updateBottomSheetPeekHeight(WindowInsetsCompat insets) {
  double width =
      getScreenWidth(getActivity())
          + insets.getSystemWindowInsetLeft()
          + insets.getSystemWindowInsetRight();
  double height =
      getScreenHeight(getActivity())
          + insets.getSystemWindowInsetTop()
          + insets.getSystemWindowInsetBottom();
  double mapHeight = width / COLLAPSED_MAP_ASPECT_RATIO;
  double peekHeight = height - mapHeight;
  bottomSheetBehavior.setPeekHeight((int) peekHeight);
}
 
Example #24
Source File: MainActivity.java    From ChromeLikeTabSwitcher with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a listener, which allows to apply the window insets to the tab switcher's padding.
 *
 * @return The listener, which has been created, as an instance of the type {@link
 * OnApplyWindowInsetsListener}. The listener may not be nullFG
 */
@NonNull
private OnApplyWindowInsetsListener createWindowInsetsListener() {
    return new OnApplyWindowInsetsListener() {

        @Override
        public WindowInsetsCompat onApplyWindowInsets(final View v,
                                                      final WindowInsetsCompat insets) {
            int left = insets.getSystemWindowInsetLeft();
            int top = insets.getSystemWindowInsetTop();
            int right = insets.getSystemWindowInsetRight();
            int bottom = insets.getSystemWindowInsetBottom();
            tabSwitcher.setPadding(left, top, right, bottom);
            float touchableAreaTop = top;

            if (tabSwitcher.getLayout() == Layout.TABLET) {
                touchableAreaTop += getResources()
                        .getDimensionPixelSize(R.dimen.tablet_tab_container_height);
            }

            RectF touchableArea = new RectF(left, touchableAreaTop,
                    getDisplayWidth(MainActivity.this) - right, touchableAreaTop +
                    ThemeUtil.getDimensionPixelSize(MainActivity.this, R.attr.actionBarSize));
            tabSwitcher.addDragGesture(
                    new SwipeGesture.Builder().setTouchableArea(touchableArea).create());
            tabSwitcher.addDragGesture(
                    new PullDownGesture.Builder().setTouchableArea(touchableArea).create());
            return insets;
        }

    };
}
 
Example #25
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public AppBarLayout(@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();
  setOrientation(VERTICAL);

  if (Build.VERSION.SDK_INT >= 21) {
    // Use the bounds view outline provider so that we cast a shadow, even without a
    // background
    ViewUtilsLollipop.setBoundsViewOutlineProvider(this);

    // If we're running on API 21+, we should reset any state list animator from our
    // default style
    ViewUtilsLollipop.setStateListAnimatorFromAttrs(
        this, attrs, defStyleAttr, DEF_STYLE_RES);
  }

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

  ViewCompat.setBackground(this, a.getDrawable(R.styleable.AppBarLayout_android_background));

  if (getBackground() instanceof ColorDrawable) {
    ColorDrawable background = (ColorDrawable) getBackground();
    MaterialShapeDrawable materialShapeDrawable = new MaterialShapeDrawable();
    materialShapeDrawable.setFillColor(ColorStateList.valueOf(background.getColor()));
    materialShapeDrawable.initializeElevationOverlay(context);
    ViewCompat.setBackground(this, materialShapeDrawable);
  }

  if (a.hasValue(R.styleable.AppBarLayout_expanded)) {
    setExpanded(
        a.getBoolean(R.styleable.AppBarLayout_expanded, false),
        false, /* animate */
        false /* force */);
  }

  if (Build.VERSION.SDK_INT >= 21 && a.hasValue(R.styleable.AppBarLayout_elevation)) {
    ViewUtilsLollipop.setDefaultAppBarLayoutStateListAnimator(
        this, a.getDimensionPixelSize(R.styleable.AppBarLayout_elevation, 0));
  }

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // In O+, we have these values set in the style. Since there is no defStyleAttr for
    // AppBarLayout at the AppCompat level, check for these attributes here.
    if (a.hasValue(R.styleable.AppBarLayout_android_keyboardNavigationCluster)) {
      this.setKeyboardNavigationCluster(
          a.getBoolean(R.styleable.AppBarLayout_android_keyboardNavigationCluster, false));
    }
    if (a.hasValue(R.styleable.AppBarLayout_android_touchscreenBlocksFocus)) {
      this.setTouchscreenBlocksFocus(
          a.getBoolean(R.styleable.AppBarLayout_android_touchscreenBlocksFocus, false));
    }
  }

  liftOnScroll = a.getBoolean(R.styleable.AppBarLayout_liftOnScroll, false);
  liftOnScrollTargetViewId =
      a.getResourceId(R.styleable.AppBarLayout_liftOnScrollTargetViewId, View.NO_ID);

  setStatusBarForeground(a.getDrawable(R.styleable.AppBarLayout_statusBarForeground));
  a.recycle();

  ViewCompat.setOnApplyWindowInsetsListener(
      this,
      new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
          return onWindowInsetChanged(insets);
        }
      });
}
 
Example #26
Source File: AppBarRelativeLayout.java    From cathode with Apache License 2.0 4 votes vote down vote up
public AppBarRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);

  setWillNotDraw(false);

  textHelper = new CollapsingTextHelper(this);
  textHelper.setExpandedTextGravity(Gravity.LEFT | Gravity.BOTTOM);
  textHelper.setTextSizeInterpolator(new DecelerateInterpolator());

  TypedArray a =
      context.obtainStyledAttributes(attrs, R.styleable.AppBarRelativeLayout, defStyleAttr,
          R.style.AppBarRelativeLayout);

  contentTopViewId =
      a.getResourceId(R.styleable.AppBarRelativeLayout_contentTopViewId, R.id.appBarContent);

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

  if (a.hasValue(R.styleable.AppBarRelativeLayout_expandedTitleMarginStart)) {
    expandedMarginStart =
        a.getDimensionPixelSize(R.styleable.AppBarRelativeLayout_expandedTitleMarginStart, 0);
  }
  if (a.hasValue(R.styleable.AppBarRelativeLayout_expandedTitleMarginEnd)) {
    expandedMarginEnd =
        a.getDimensionPixelSize(R.styleable.AppBarRelativeLayout_expandedTitleMarginEnd, 0);
  }

  if (a.hasValue(R.styleable.AppBarRelativeLayout_expandedTitleMarginTop)) {
    expandedMarginTop =
        a.getDimensionPixelSize(R.styleable.AppBarRelativeLayout_expandedTitleMarginTop, 0);
  }

  if (a.hasValue(R.styleable.AppBarRelativeLayout_expandedTitleMarginBottom)) {
    expandedMarginBottom =
        a.getDimensionPixelSize(R.styleable.AppBarRelativeLayout_expandedTitleMarginBottom, 0);
  }

  final int collapsedTextAppearance =
      a.getResourceId(R.styleable.AppBarRelativeLayout_collapsedTitleTextAppearance,
          R.style.TextAppearance_AppCompat_Widget_ActionBar_Title);
  textHelper.setCollapsedTextAppearance(collapsedTextAppearance);

  final int expandedTextAppearance =
      a.getResourceId(R.styleable.AppBarRelativeLayout_expandedTitleTextAppearance,
          R.style.TextAppearance_AppCompat_Title);
  textHelper.setExpandedTextAppearance(expandedTextAppearance);

  contentScrim = a.getDrawable(R.styleable.AppBarRelativeLayout_contentScrim);
  if (contentScrim != null) {
    contentScrim.setCallback(this);
  }
  statusBarScrim = a.getDrawable(R.styleable.AppBarRelativeLayout_statusBarScrim);
  if (statusBarScrim != null) {
    statusBarScrim.setCallback(this);
  }

  a.recycle();

  ViewCompat.setOnApplyWindowInsetsListener(this,
      new androidx.core.view.OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
          lastInsets = insets;
          insetsTop = lastInsets.getSystemWindowInsetTop();
          return insets.consumeSystemWindowInsets();
        }
      });
}
 
Example #27
Source File: BaseActivity.java    From OneText_For_Android with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPreferences = getSharedPreferences("setting", MODE_PRIVATE);
    editor = sharedPreferences.edit();
    //Q导航栏沉浸
    rootview = findViewById(android.R.id.content);

    /*ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(), new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            v.setPadding(0,0,0,insets.getSystemWindowInsetBottom());
            return insets;
        }
    });*/
    //状态栏icon黑色
    int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
    if (mode == Configuration.UI_MODE_NIGHT_NO) {
        this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR | View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR);
    }
    rootview.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
    ViewCompat.setOnApplyWindowInsetsListener(rootview, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
            rootview.setPadding(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), 0);
            return insets;
        }
    });
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
        //rootView.setFitsSystemWindows(true);
        //rootView.setPadding(0,0,0,getNavigationBarHeight());
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    }
    //rootView.setFitsSystemWindows(true);
    /*if ((Build.VERSION.SDK_INT<Build.VERSION_CODES.Q)|(getNavigationBarHeight()>dp2px(16))) {
        //rootView.setPadding(0,0,0,getNavigationBarHeight());
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
            //rootView.setFitsSystemWindows(true);
            rootView.setPadding(0,0,0,getNavigationBarHeight());
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }else {
            //rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            //rootView.setFitsSystemWindows(true);
        }
    }else {
        rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
    }*/
    //设置为miui主题
    //setMiuiTheme(BaseActivity.this,0,mode);
    //getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    //全局自定义字体
    ViewPump.init(ViewPump.builder()
            .addInterceptor(new CalligraphyInterceptor(
                    new CalligraphyConfig.Builder()
                            .setDefaultFontPath(sharedPreferences.getString("font_path", null))
                            .setFontAttrId(R.attr.fontPath)
                            .build()))
            .build());
}
 
Example #28
Source File: ViewPagerActivity.java    From FlexibleAdapter with Apache License 2.0 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_pager);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    HeaderView headerView = (HeaderView) findViewById(R.id.toolbar_header_view);
    headerView.bindTo(getString(R.string.app_name), getString(R.string.viewpager));

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.view_pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    //Coordinatorlayout Status Bar Padding Disappears From Viewpager 2nd-page
    //http://stackoverflow.com/questions/31368781/coordinatorlayout-status-bar-padding-disappears-from-viewpager-2nd-page
    ViewCompat.setOnApplyWindowInsetsListener(mViewPager, new OnApplyWindowInsetsListener() {
        @Override
        public WindowInsetsCompat onApplyWindowInsets(View v,
                                                      WindowInsetsCompat insets) {
            insets = ViewCompat.onApplyWindowInsets(v, insets);
            if (insets.isConsumed()) {
                return insets;
            }

            boolean consumed = false;
            for (int i = 0, count = mViewPager.getChildCount(); i < count; i++) {
                ViewCompat.dispatchApplyWindowInsets(mViewPager.getChildAt(i), insets);
                if (insets.isConsumed()) {
                    consumed = true;
                }
            }
            return consumed ? insets.consumeSystemWindowInsets() : insets;
        }
    });
}
 
Example #29
Source File: FeatureDetailsFragment.java    From ground-android with Apache License 2.0 4 votes vote down vote up
private void onApplyWindowInsets(WindowInsetsCompat insets) {
  observationListContainer.setPadding(0, 0, 0, insets.getSystemWindowInsetBottom());
}
 
Example #30
Source File: NavigationTestView.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onInsetsChanged(WindowInsetsCompat insets) {
  super.onInsetsChanged(insets);
  hasSystemWindowInsets = insets.hasSystemWindowInsets();
}