Java Code Examples for androidx.coordinatorlayout.widget.CoordinatorLayout#LayoutParams

The following examples show how to use androidx.coordinatorlayout.widget.CoordinatorLayout#LayoutParams . 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: MainFrameActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
public void updateFabGravity() {
    CoordinatorLayout.LayoutParams mainFabParams = (CoordinatorLayout.LayoutParams) mMainFab.getLayoutParams();
    CoordinatorLayout.LayoutParams notiFabParams = (CoordinatorLayout.LayoutParams) mNotiificationFab.getLayoutParams();
    if (HiSettingsHelper.getInstance().isFabLeftSide()) {
        mainFabParams.anchorGravity = Gravity.BOTTOM | Gravity.LEFT | Gravity.END;
    } else {
        mainFabParams.anchorGravity = Gravity.BOTTOM | Gravity.RIGHT | Gravity.END;
    }
    if (HiSettingsHelper.getInstance().isFabAutoHide()) {
        mainFabParams.setBehavior(new FABHideOnScrollBehavior());
        notiFabParams.setBehavior(new FABHideOnScrollBehavior());
    } else {
        mainFabParams.setBehavior(null);
        notiFabParams.setBehavior(null);
        mMainFab.show();
    }
}
 
Example 2
Source File: AppBarWithAnchoredFabMarginsTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFabRightMargin() throws Throwable {
  configureContent(
      R.layout.design_appbar_anchored_fab_margin_right,
      R.string.design_appbar_anchored_fab_margin_right);

  final FloatingActionButton fab = mCoordinatorLayout.findViewById(R.id.fab);
  final CoordinatorLayout.LayoutParams fabLp =
      (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
  assertEquals(mAppBar.getId(), fabLp.getAnchorId());

  final int[] appbarOnScreenXY = new int[2];
  final int[] fabOnScreenXY = new int[2];
  mAppBar.getLocationOnScreen(appbarOnScreenXY);
  fab.getLocationOnScreen(fabOnScreenXY);

  // FAB is right-aligned in the coordinate system of its anchor (app bar). In addition,
  // its right margin "pushes" it away in the coordinate system of the parent
  // (CoordinatorLayout)
  assertEquals(
      appbarOnScreenXY[0] + mAppBar.getWidth() - fabMargin, fabOnScreenXY[0] + fab.getWidth(), 1);
  // FAB's vertical center should be aligned with the bottom edge of its anchor (app bar).
  assertEquals(
      appbarOnScreenXY[1] + mAppBar.getHeight(), fabOnScreenXY[1] + fab.getHeight() / 2, 1);
}
 
Example 3
Source File: FabPresenter.java    From react-native-navigation with MIT License 6 votes vote down vote up
private void setParams(ViewController component, View fab, FabOptions options) {
    CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    lp.rightMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
    lp.leftMargin = (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
    lp.bottomMargin = component.getBottomInset() + (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin);
    fab.setTag(R.id.fab_bottom_margin, (int) viewGroup.getContext().getResources().getDimension(R.dimen.margin));
    lp.gravity = Gravity.BOTTOM;
    if (options.alignHorizontally.hasValue()) {
        if ("right".equals(options.alignHorizontally.get())) {
            lp.gravity |= Gravity.RIGHT;
        }
        if ("left".equals(options.alignHorizontally.get())) {
            lp.gravity |= Gravity.LEFT;
        }
    } else {
        lp.gravity |= Gravity.RIGHT;
    }
    fab.setLayoutParams(lp);
}
 
Example 4
Source File: DraggableCardFragment.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean performAccessibilityAction(View host, int action, Bundle arguments) {
  int gravity;
  if (action == R.id.move_card_top_left_action) {
    gravity = Gravity.TOP | Gravity.LEFT;
  } else if (action == R.id.move_card_top_right_action) {
    gravity = Gravity.TOP | Gravity.RIGHT;
  } else if (action == R.id.move_card_bottom_left_action) {
    gravity = Gravity.BOTTOM | Gravity.LEFT;
  } else if (action == R.id.move_card_bottom_right_action) {
    gravity = Gravity.BOTTOM | Gravity.RIGHT;
  } else if (action == R.id.move_card_center_action) {
    gravity = Gravity.CENTER;
  } else {
    return super.performAccessibilityAction(host, action, arguments);
  }

  CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) card
      .getLayoutParams();
  if (layoutParams.gravity != gravity) {
    layoutParams.gravity = gravity;
    card.requestLayout();
  }

  return true;
}
 
Example 5
Source File: ViewUtils.java    From DevUtils with Apache License 2.0 6 votes vote down vote up
/**
 * 获取 View Layout Gravity
 * @param view {@link View}
 * @return Layout Gravity
 */
public static int getLayoutGravity(final View view) {
    if (view != null) {
        try {
            if (view.getLayoutParams() instanceof LinearLayout.LayoutParams) {
                return ((LinearLayout.LayoutParams) view.getLayoutParams()).gravity;
            } else if (view.getLayoutParams() instanceof FrameLayout.LayoutParams) {
                return ((FrameLayout.LayoutParams) view.getLayoutParams()).gravity;
            } else if (view.getLayoutParams() instanceof ViewPager.LayoutParams) {
                return ((ViewPager.LayoutParams) view.getLayoutParams()).gravity;
            } else if (view.getLayoutParams() instanceof CoordinatorLayout.LayoutParams) {
                return ((CoordinatorLayout.LayoutParams) view.getLayoutParams()).gravity;
            } else if (view.getLayoutParams() instanceof DrawerLayout.LayoutParams) {
                return ((DrawerLayout.LayoutParams) view.getLayoutParams()).gravity;
            } else {
                // 抛出不支持的类型
                throw new Exception("layoutParams:" + view.getLayoutParams().toString());
            }
        } catch (Exception e) {
            LogPrintUtils.eTag(TAG, e, "getLayoutGravity");
        }
    }
    return 0;
}
 
Example 6
Source File: MapActivity.java    From nearby-android with Apache License 2.0 6 votes vote down vote up
/**
 * Show the list of directions
 * @param directions List of DirectionManeuver items containing navigation directions
 */
public final void showDirections(final List<DirectionManeuver> directions){
  final FragmentManager fm = getSupportFragmentManager();
  RouteDirectionsFragment routeDirectionsFragment = (RouteDirectionsFragment) fm.findFragmentById(R.id.route_directions_container);
  if (routeDirectionsFragment == null){
    routeDirectionsFragment = RouteDirectionsFragment.newInstance();
    ActivityUtils.addFragmentToActivity(
        getSupportFragmentManager(), routeDirectionsFragment, R.id.route_directions_container, getString(R.string.route_fragment));
  }
  // Show the fragment
  final LinearLayout layout = findViewById(R.id.route_directions_container);
  layout.setLayoutParams(new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
      ViewGroup.LayoutParams.MATCH_PARENT));
  layout.requestLayout();

  // Hide the map
  final FrameLayout mapLayout = findViewById(R.id.map_fragment_container);
  final CoordinatorLayout.LayoutParams  layoutParams  =  new CoordinatorLayout.LayoutParams(0,0);
  layoutParams.setMargins(0, 0,0,0);
  mapLayout.setLayoutParams(layoutParams);
  mapLayout.requestLayout();

  routeDirectionsFragment.setRoutingDirections(directions);
}
 
Example 7
Source File: CoordinatorSnackbarWithFabTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testBehaviorBasedSlidingFromRuntimeApiCall() {
  // Use a layout in which an AppCompatTextView child doesn't have any configured Behavior
  onView(withId(R.id.coordinator_stub))
      .perform(inflateViewStub(R.layout.design_snackbar_behavior_runtime));

  // and configure that Behavior at runtime by setting it on its LayoutParams
  final AppCompatTextView textView = mCoordinatorLayout.findViewById(R.id.text);
  final CoordinatorLayout.LayoutParams textViewLp =
      (CoordinatorLayout.LayoutParams) textView.getLayoutParams();
  textViewLp.setBehavior(new TestFloatingBehavior());

  // Create and show a snackbar
  snackbar =
      Snackbar.make(mCoordinatorLayout, MESSAGE_TEXT, Snackbar.LENGTH_INDEFINITE)
          .setAction(ACTION_TEXT, mock(View.OnClickListener.class));
  SnackbarUtils.showTransientBottomBarAndWaitUntilFullyShown(snackbar);

  verifySnackbarViewStacking(textView, 0);
}
 
Example 8
Source File: CustomBottomSheetDialogFragment.java    From Weather with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void setupDialog(Dialog dialog, int style) {
    //super.setupDialog(dialog, style);
    View contentView = View.inflate(getContext(), R.layout.dialog_modal, null);
    dialog.setContentView(contentView);
    CoordinatorLayout.LayoutParams layoutParams =
            (CoordinatorLayout.LayoutParams) ((View) contentView.getParent()).getLayoutParams();
    CoordinatorLayout.Behavior behavior = layoutParams.getBehavior();
    if (behavior != null && behavior instanceof BottomSheetBehavior) {
        ((BottomSheetBehavior) behavior).setBottomSheetCallback(mBottomSheetBehaviorCallback);

    }
}
 
Example 9
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Pre-Lollipop we use padding so that the shadow has enough space to be drawn. This method
 * offsets our layout position so that we're positioned correctly if we're on one of our
 * parent's edges.
 */
private void offsetIfNeeded(
    @NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab) {
  final Rect padding = fab.shadowPadding;

  if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) {
    final CoordinatorLayout.LayoutParams lp =
        (CoordinatorLayout.LayoutParams) fab.getLayoutParams();

    int offsetTB = 0;
    int offsetLR = 0;

    if (fab.getRight() >= parent.getWidth() - lp.rightMargin) {
      // If we're on the right edge, shift it the right
      offsetLR = padding.right;
    } else if (fab.getLeft() <= lp.leftMargin) {
      // If we're on the left edge, shift it the left
      offsetLR = -padding.left;
    }
    if (fab.getBottom() >= parent.getHeight() - lp.bottomMargin) {
      // If we're on the bottom edge, shift it down
      offsetTB = padding.bottom;
    } else if (fab.getTop() <= lp.topMargin) {
      // If we're on the top edge, shift it up
      offsetTB = -padding.top;
    }

    if (offsetTB != 0) {
      ViewCompat.offsetTopAndBottom(fab, offsetTB);
    }
    if (offsetLR != 0) {
      ViewCompat.offsetLeftAndRight(fab, offsetLR);
    }
  }
}
 
Example 10
Source File: BottomAppBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent, @NonNull BottomAppBar child, int layoutDirection) {
  viewRef = new WeakReference<>(child);

  View dependentView = child.findDependentView();
  if (dependentView != null && !ViewCompat.isLaidOut(dependentView)) {
    // Set the initial position of the FloatingActionButton with the BottomAppBar vertical
    // offset.
    CoordinatorLayout.LayoutParams fabLayoutParams =
        (CoordinatorLayout.LayoutParams) dependentView.getLayoutParams();
    fabLayoutParams.anchorGravity = Gravity.CENTER | Gravity.TOP;

    // Keep track of the original bottom margin for the fab. We will manage the margin if
    // nothing was set.
    originalBottomMargin = fabLayoutParams.bottomMargin;

    if (dependentView instanceof FloatingActionButton) {
      FloatingActionButton fab = ((FloatingActionButton) dependentView);

      // Always update the BAB if the fab is laid out.
      fab.addOnLayoutChangeListener(fabLayoutListener);

      // Ensure the FAB is correctly linked to this BAB so the animations can run correctly
      child.addFabAnimationListeners(fab);
    }

    // Move the fab to the correct position
    child.setCutoutState();
  }

  // Now let the CoordinatorLayout lay out the BAB
  parent.onLayoutChild(child, layoutDirection);
  return super.onLayoutChild(parent, child, layoutDirection);
}
 
Example 11
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onMeasureChild(
    @NonNull CoordinatorLayout parent,
    @NonNull T child,
    int parentWidthMeasureSpec,
    int widthUsed,
    int parentHeightMeasureSpec,
    int heightUsed) {
  final CoordinatorLayout.LayoutParams lp =
      (CoordinatorLayout.LayoutParams) child.getLayoutParams();
  if (lp.height == CoordinatorLayout.LayoutParams.WRAP_CONTENT) {
    // If the view is set to wrap on it's height, CoordinatorLayout by default will
    // cap the view at the CoL's height. Since the AppBarLayout can scroll, this isn't
    // what we actually want, so we measure it ourselves with an unspecified spec to
    // allow the child to be larger than it's parent
    parent.onMeasureChild(
        child,
        parentWidthMeasureSpec,
        widthUsed,
        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
        heightUsed);
    return true;
  }

  // Let the parent handle it as normal
  return super.onMeasureChild(
      parent, child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
}
 
Example 12
Source File: ExtendedFloatingActionButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams lp) {
  if (lp.dodgeInsetEdges == Gravity.NO_GRAVITY) {
    // If the developer hasn't set dodgeInsetEdges, lets set it to BOTTOM so that
    // we dodge any Snackbars
    lp.dodgeInsetEdges = Gravity.BOTTOM;
  }
}
 
Example 13
Source File: ExtendedFloatingActionButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private static boolean isBottomSheet(@NonNull View view) {
  final ViewGroup.LayoutParams lp = view.getLayoutParams();
  if (lp instanceof CoordinatorLayout.LayoutParams) {
    return ((CoordinatorLayout.LayoutParams) lp).getBehavior() instanceof BottomSheetBehavior;
  }
  return false;
}
 
Example 14
Source File: ArticleTabFragment.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 5 votes vote down vote up
private void updateFloatingMenu() {
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) mFam.getLayoutParams();
    mBehavior = (ScrollAwareFamBehavior) lp.getBehavior();
    if (mConfig.isLeftHandMode()) {
        lp.gravity = Gravity.START | Gravity.BOTTOM;
        mFam.setExpandDirection(FloatingActionsMenu.EXPAND_UP, FloatingActionsMenu.LABELS_ON_RIGHT_SIDE);
        mFam.setLayoutParams(lp);
    }
}
 
Example 15
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onAttachedToLayoutParams(@NonNull CoordinatorLayout.LayoutParams layoutParams) {
    super.onAttachedToLayoutParams(layoutParams);
    // These may already be null, but just be safe, explicitly assign them. This lets us know the
    // first time we layout with this behavior by checking (viewRef == null).
    viewRef = null;
    viewDragHelper = null;
}
 
Example 16
Source File: ViewHelper.java    From candybar with Apache License 2.0 5 votes vote down vote up
public static void resetViewBottomMargin(@Nullable View view) {
    if (view == null) return;

    Context context = ContextHelper.getBaseContext(view);
    int orientation = context.getResources().getConfiguration().orientation;

    if (!(view.getLayoutParams() instanceof CoordinatorLayout.LayoutParams))
        return;

    CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
    int left = params.leftMargin;
    int right = params.rightMargin;
    int bottom = params.bottomMargin;
    int top = params.topMargin;
    int bottomNavBar = 0;
    int rightNavBar = 0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        boolean tabletMode = context.getResources().getBoolean(R.bool.android_helpers_tablet_mode);
        if (tabletMode || orientation == Configuration.ORIENTATION_PORTRAIT) {
            bottomNavBar = WindowHelper.getNavigationBarHeight(context);
        } else {
            rightNavBar = WindowHelper.getNavigationBarHeight(context);
        }
    }

    int navBar = WindowHelper.getNavigationBarHeight(context);
    if ((bottom > bottomNavBar) && ((bottom - navBar) > 0))
        bottom -= navBar;
    if ((right > rightNavBar) && ((right - navBar) > 0))
        right -= navBar;

    params.setMargins(left, top, (right + rightNavBar), (bottom + bottomNavBar));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        params.setMarginEnd((right + rightNavBar));
    }
    view.setLayoutParams(params);
}
 
Example 17
Source File: BottomSheetBehaviorTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void withFabVisibilityChange(boolean shown, Runnable action) {
  OnVisibilityChangedListener listener = new OnVisibilityChangedListener(shown);
  CoordinatorLayout.LayoutParams lp =
      (CoordinatorLayout.LayoutParams) activityTestRule.getActivity().mFab.getLayoutParams();
  FloatingActionButton.Behavior behavior = (FloatingActionButton.Behavior) lp.getBehavior();
  behavior.setInternalAutoHideListener(listener);
  Espresso.registerIdlingResources(listener);
  try {
    action.run();
  } finally {
    Espresso.unregisterIdlingResources(listener);
  }
}
 
Example 18
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private boolean updateFabVisibilityForBottomSheet(
    @NonNull View bottomSheet, @NonNull FloatingActionButton child) {
  if (!shouldUpdateVisibility(bottomSheet, child)) {
    return false;
  }
  CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
  if (bottomSheet.getTop() < child.getHeight() / 2 + lp.topMargin) {
    child.hide(internalAutoHideListener, false);
  } else {
    child.show(internalAutoHideListener, false);
  }
  return true;
}
 
Example 19
Source File: FabScrollBehavior.java    From SmsCode with GNU General Public License v3.0 4 votes vote down vote up
private void animateOut(FloatingActionButton fab) {
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    int bottomMargin = layoutParams.bottomMargin;
    fab.animate().translationY(fab.getHeight() + bottomMargin).setInterpolator(new LinearInterpolator()).start();
}
 
Example 20
Source File: CoordinatorLayoutUtils.java    From react-native-navigation with MIT License 4 votes vote down vote up
public static CoordinatorLayout.LayoutParams matchParentWithBehaviour(CoordinatorLayout.Behavior behavior) {
    CoordinatorLayout.LayoutParams lp = new CoordinatorLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
    lp.setBehavior(behavior);
    return lp;
}