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

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

  final FloatingActionButton fab = mCoordinatorLayout.findViewById(R.id.fab);
  final FloatingActionButton fab2 = mCoordinatorLayout.findViewById(R.id.fab2);

  final int[] fabOnScreenXY = new int[2];
  final int[] fab2OnScreenXY = new int[2];
  fab.getLocationOnScreen(fabOnScreenXY);
  fab2.getLocationOnScreen(fab2OnScreenXY);

  final Rect fabRect = new Rect();
  final Rect fab2Rect = new Rect();
  fab.getContentRect(fabRect);
  fab2.getContentRect(fab2Rect);

  // Our second FAB is configured to "dodge" the first one - to be displayed to the
  // right of it
  int firstRight = fabOnScreenXY[0] + fabRect.right;
  int secondLeft = fab2OnScreenXY[0] + fab2Rect.left;
  assertTrue(
      "Second button left edge at "
          + secondLeft
          + " should be dodging the first button right edge at "
          + firstRight,
      secondLeft >= firstRight);
}
 
Example 2
Source File: AppBarWithDodgingTest.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testRightDodge() throws Throwable {
  configureContent(R.layout.design_appbar_dodge_right, R.string.design_appbar_dodge_right);

  final FloatingActionButton fab = mCoordinatorLayout.findViewById(R.id.fab);
  final FloatingActionButton fab2 = mCoordinatorLayout.findViewById(R.id.fab2);

  final int[] fabOnScreenXY = new int[2];
  final int[] fab2OnScreenXY = new int[2];
  fab.getLocationOnScreen(fabOnScreenXY);
  fab2.getLocationOnScreen(fab2OnScreenXY);

  final Rect fabRect = new Rect();
  final Rect fab2Rect = new Rect();
  fab.getContentRect(fabRect);
  fab2.getContentRect(fab2Rect);

  // Our second FAB is configured to "dodge" the first one - to be displayed to the
  // left of it
  int firstLeft = fabOnScreenXY[0] + fabRect.left;
  int secondRight = fab2OnScreenXY[0] + fab2Rect.right;
  assertTrue(
      "Second button right edge at "
          + secondRight
          + " should be dodging the first button left edge at "
          + firstLeft,
      secondRight <= firstLeft);
}
 
Example 3
Source File: TestUtilsMatchers.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/** Returns a matcher that matches FloatingActionButtons with the specified content height */
public static Matcher<View> withFabContentHeight(final int size) {
  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 Rect area = new Rect();
      fab.getContentRect(area);

      if (area.height() != size) {
        failedCheckDescription =
            "Content height " + area.height() + " is different than expected " + size;
        return false;
      }

      return true;
    }
  };
}
 
Example 4
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 background fill color.
 */
public static Matcher<View> withFabBackgroundFill(final @ColorInt int fillColor) {
  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;

      // Since the FAB background is round, and may contain the shadow, we'll look at
      // just the center half rect of the content area
      final Rect area = new Rect();
      fab.getContentRect(area);

      final int rectHeightQuarter = area.height() / 4;
      final int rectWidthQuarter = area.width() / 4;
      area.left += rectWidthQuarter;
      area.top += rectHeightQuarter;
      area.right -= rectWidthQuarter;
      area.bottom -= rectHeightQuarter;

      try {
        TestUtils.assertAllPixelsOfColor(
            "",
            fab.getBackground(),
            view.getWidth(),
            view.getHeight(),
            false,
            fillColor,
            area,
            0,
            true);
      } catch (Throwable t) {
        failedCheckDescription = t.getMessage();
        return false;
      }
      return true;
    }
  };
}
 
Example 5
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;
      }
    }
  };
}