Skip to content

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;
  }
}

@pubiqq pubiqq force-pushed the button/fix-insets branch from 90a0740 to c06f058 Compare May 14, 2024 23:13
@pubiqq pubiqq requested a review from leticiarossi May 15, 2024 18:10
@pubiqq pubiqq force-pushed the button/fix-insets branch from c06f058 to eb718a7 Compare August 13, 2024 11:46
@pubiqq pubiqq mentioned this pull request Sep 23, 2024
@pubiqq pubiqq force-pushed the button/fix-insets branch from eb718a7 to e8e56a7 Compare May 12, 2025 20:28
@pubiqq pubiqq force-pushed the button/fix-insets branch from e8e56a7 to 4b20203 Compare June 26, 2025 13:22
@pubiqq pubiqq force-pushed the button/fix-insets branch 2 times, most recently from 4dc4d1a to 888e6bb Compare July 30, 2025 19:41
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