Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Button] Fix inset attributes working incorrectly in RTL #4164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

pubiqq
Copy link
Contributor

@pubiqq pubiqq commented May 7, 2024

Fixes #4163

ViewCompat.setPaddingRelative(
materialButton,
paddingStart + insetLeft,
materialButton.setPadding(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this and below setPadding calls will break any existing usages of paddingStart and paddingEnd in RTL so I don't think we can do that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you give more details about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've written a small test comparing the current and new approach, maybe it will help you to describe the issue:

package com.google.android.material;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.view.View;

import androidx.test.core.app.ApplicationProvider;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class ViewPaddingTest {

  private final Context context = ApplicationProvider.getApplicationContext();

  @Test
  public void test() {
    View view1 = new View(context);
    View view2 = new View(context);

    view1.setPadding(1, 2, 3, 4);
    view2.setPadding(1, 2, 3, 4);

    currentStoreAndLoadPaddings(view1);
    newStoreAndLoadPaddings(view2);

    assertThat(view2.getPaddingLeft()).isEqualTo(view1.getPaddingLeft());
    assertThat(view2.getPaddingStart()).isEqualTo(view1.getPaddingStart());
    assertThat(view2.getPaddingTop()).isEqualTo(view1.getPaddingTop());
    assertThat(view2.getPaddingRight()).isEqualTo(view1.getPaddingRight());
    assertThat(view2.getPaddingEnd()).isEqualTo(view1.getPaddingEnd());
    assertThat(view2.getPaddingBottom()).isEqualTo(view1.getPaddingBottom());
  }

  /**
   * <a href="https://github.com/material-components/material-components-android/blob/9b6ceac72a084bf187f6e3238dd9283e861196c3/lib/java/com/google/android/material/button/MaterialButtonHelper.java#L123-L141">MaterialButtonHelper.java#L123-L141 from the <code>master</code> branch</a>
   */
  private void currentStoreAndLoadPaddings(View view) {
    int paddingStart = view.getPaddingStart();
    int paddingTop = view.getPaddingTop();
    int paddingEnd = view.getPaddingEnd();
    int paddingBottom = view.getPaddingBottom();

    view.setBackground(createTestDrawableWithPaddings());

    view.setPaddingRelative(paddingStart, paddingTop, paddingEnd, paddingBottom);
  }

  /**
   * <a href="https://github.com/material-components/material-components-android/blob/c06f058d7c478b82aaf5c7eddce0a3a54ce3f50c/lib/java/com/google/android/material/button/MaterialButtonHelper.java#L123-L141">MaterialButtonHelper.java#L123-L141 from the PR</a>
   */
  private void newStoreAndLoadPaddings(View view) {
    int paddingLeft = view.getPaddingLeft();
    int paddingTop = view.getPaddingTop();
    int paddingRight = view.getPaddingRight();
    int paddingBottom = view.getPaddingBottom();

    view.setBackground(createTestDrawableWithPaddings());

    view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
  }

  private Drawable createTestDrawableWithPaddings() {
    ShapeDrawable testDrawable = new ShapeDrawable();
    testDrawable.setPadding(10, 11, 12, 13);
    return testDrawable;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Button] Buttons with insets are broken in RTL mode
2 participants