Java Code Examples for com.google.android.material.floatingactionbutton.FloatingActionButton#getLayoutParams()

The following examples show how to use com.google.android.material.floatingactionbutton.FloatingActionButton#getLayoutParams() . 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: AppBarWithAnchoredFabMarginsTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFabBottomMargin() throws Throwable {
  configureContent(
      R.layout.design_appbar_anchored_fab_margin_bottom,
      R.string.design_appbar_anchored_fab_margin_bottom);

  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 horizontally centered in the coordinate system of its anchor (app bar).
  assertEquals(
      appbarOnScreenXY[0] + mAppBar.getWidth() / 2, fabOnScreenXY[0] + fab.getWidth() / 2, 1);
  // Bottom margin is in the coordinate space of the parent (CoordinatorLayout) and not
  // the anchor. Since our FAB is far enough from the bottom edge of CoordinatorLayout,
  // we are expecting the vertical center of the FAB to be aligned with the bottom edge
  // of its anchor (app bar).
  assertEquals(
      appbarOnScreenXY[1] + mAppBar.getHeight(), fabOnScreenXY[1] + fab.getHeight() / 2, 1);
}
 
Example 2
Source File: AppBarWithAnchoredFabMarginsTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFabTopMargin() throws Throwable {
  configureContent(
      R.layout.design_appbar_anchored_fab_margin_top,
      R.string.design_appbar_anchored_fab_margin_top);

  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 horizontally centered in the coordinate system of its anchor (app bar).
  assertEquals(
      appbarOnScreenXY[0] + mAppBar.getWidth() / 2, fabOnScreenXY[0] + fab.getWidth() / 2, 1);
  // Top margin is in the coordinate space of the parent (CoordinatorLayout) and not
  // the anchor. Since our FAB is far enough from the bottom edge of CoordinatorLayout,
  // we are expecting the vertical center of the FAB to 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: AppBarWithAnchoredFabMarginsTest.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Test
public void testFabLeftMargin() throws Throwable {
  configureContent(
      R.layout.design_appbar_anchored_fab_margin_left,
      R.string.design_appbar_anchored_fab_margin_left);

  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 left-aligned in the coordinate system of its anchor (app bar). In addition,
  // its left margin "pushes" it away in the coordinate system of the parent
  // (CoordinatorLayout)
  assertEquals(appbarOnScreenXY[0] + fabMargin, fabOnScreenXY[0], 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 4
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 5
Source File: BottomNavigationBar.java    From BottomNavigation with Apache License 2.0 5 votes vote down vote up
public void setFab(FloatingActionButton fab) {
    ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
    if (layoutParams != null && layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        BottomNavBarFabBehaviour bottomNavBarFabBehaviour = new BottomNavBarFabBehaviour();
        coLayoutParams.setBehavior(bottomNavBarFabBehaviour);
    }
}
 
Example 6
Source File: BottomNavigationBar.java    From BottomNavigation with Apache License 2.0 5 votes vote down vote up
private void setFab(FloatingActionButton fab, @FabBehaviour int fabBehaviour) {
    ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
    if (layoutParams != null && layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        BottomNavBarFabBehaviour bottomNavBarFabBehaviour = new BottomNavBarFabBehaviour();
        coLayoutParams.setBehavior(bottomNavBarFabBehaviour);
    }
}
 
Example 7
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 8
Source File: FabScrollBehavior.java    From XposedSmsCode 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 9
Source File: TestUtilsMatchers.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/** Returns a matcher that matches FloatingActionButtons with the specified gravity. */
public static Matcher<View> withFabContentAreaOnMargins(final int gravity) {
  return new BoundedMatcher<View, View>(View.class) {
    private String failedCheckDescription;

    @Override
    public void describeTo(final Description description) {
      description.appendText(failedCheckDescription);
    }

    @Override
    public boolean matchesSafely(final View view) {
      if (!(view instanceof FloatingActionButton)) {
        return false;
      }

      final FloatingActionButton fab = (FloatingActionButton) view;
      final ViewGroup.MarginLayoutParams lp =
          (ViewGroup.MarginLayoutParams) fab.getLayoutParams();
      final ViewGroup parent = (ViewGroup) view.getParent();

      final Rect area = new Rect();
      fab.getContentRect(area);

      final int absGravity =
          GravityCompat.getAbsoluteGravity(gravity, ViewCompat.getLayoutDirection(view));

      try {
        switch (absGravity & Gravity.VERTICAL_GRAVITY_MASK) {
          case Gravity.TOP:
            assertEquals(lp.topMargin, fab.getTop() + area.top);
            break;
          case Gravity.BOTTOM:
            assertEquals(parent.getHeight() - lp.bottomMargin, fab.getTop() + area.bottom);
            break;
        }
        switch (absGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
          case Gravity.LEFT:
            assertEquals(lp.leftMargin, fab.getLeft() + area.left);
            break;
          case Gravity.RIGHT:
            assertEquals(parent.getWidth() - lp.rightMargin, fab.getLeft() + area.right);
            break;
        }
        return true;
      } catch (Throwable t) {
        failedCheckDescription = t.getMessage();
        return false;
      }
    }
  };
}
 
Example 10
Source File: FABMenu.java    From Jockey with Apache License 2.0 4 votes vote down vote up
private FloatingActionButton buildChild(@DrawableRes int icon,
                                        final OnClickListener onClickListener, String label) {
    FloatingActionButton button = LayoutInflater.from(getContext())
            .inflate(R.layout.mini_fab, (ViewGroup) getParent(), true)
            .findViewWithTag("fab-null");

    button.setTag("fab-" + label);
    button.setImageResource(icon);
    button.setVisibility(GONE);
    button.setOnClickListener(v -> {
        onClickListener.onClick(v);
        hideChildren();
    });

    if (getParent() instanceof CoordinatorLayout) {
        final float padding = getResources().getDimension(R.dimen.fab_margin);
        final float dpScale = getResources().getDisplayMetrics().density;

        CoordinatorLayout.LayoutParams params =
                (CoordinatorLayout.LayoutParams) button.getLayoutParams();
        if (ViewUtils.isRtl(getContext())) {
            params.leftMargin += padding;
        } else {
            params.rightMargin += padding;
        }
        params.bottomMargin = (int) (SIZE_L_DP * dpScale + padding * (2 + children.size())
                + SIZE_S_DP * dpScale * children.size());

        // For some reason, the children are 12dp higher and 18dp further to the left on pre-L
        // devices than on L+ devices. I don't know for sure what causes this (I suspect it's
        // the drop shadow or elevation compatibility code), but this takes care of it.
        //
        // There's probably a better way to fix this, but this was the easiest. If for some
        // reason this changes in an update to one of the support libraries, just remeasure
        // these offsets and update them here.
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            if (ViewUtils.isRtl(getContext())) {
                params.leftMargin -= 12 * dpScale;
            } else {
                params.rightMargin -= 12 * dpScale;
            }
            params.bottomMargin -= 18 * dpScale;
        }

        button.setLayoutParams(params);
    } else {
        Timber.e("Parent must be a CoordinatorLayout to properly set margin");
    }

    // When children aren't visible on screen, remove them from the view hierarchy completely
    // If we don't do this, then the FloatingActionButton Behaviors conflict for some reason
    // and Snackbars won't slide the FAB up which is kind of an important detail.
    //
    // FABMenu.Behavior takes care of some of the left over discrepancies like overlapping FAB's
    //
    // Additionally, the screen is functionally important because it prevents the user
    // from doing something that could generate a Snackbar when the FAB's are visible
    // which can cause the main FAB to be overlapped.
    ((ViewGroup) button.getParent()).removeView(button);
    return button;
}