When using app:iconGravity="textEnd" with MaterialButton, adjusting the spacing between the text and the icon (especially with dynamic widths) can be a bit tricky, but there are a few approaches to achieve the desired padding. Here's how you can do it:
1. Using app:iconPadding (Recommended for Material 1.3.0 and above)
If you're using Material Components 1.3.0 or later, the simplest way to add space between the text and the icon is by utilizing the app:iconPadding attribute. This attribute allows you to specify the padding between the icon and the text.
In your build.gradle (ensure the version is 1.3.0 or higher):
implementation 'com.google.android.material:material:1.9.0' // or any version >= 1.3.0
In your layout XML:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit Website"
app:icon="@drawable/ic_open_in_new_black_24dp"
app:iconGravity="textEnd"
app:iconPadding="8dp" <!-- Adjust this value as needed -->
/>
2. Utilizing android:paddingEnd on the Button (Simplistic Approach)
While not the most precise method for controlling the space between text and icon, adding android:paddingEnd to the MaterialButton can provide some breathing room. However, this adds padding to the entire end of the button, not just between the text and icon.
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit Website"
app:icon="@drawable/ic_open_in_new_black_24dp"
app:iconGravity="textEnd"
android:paddingEnd="16dp" <!-- Affects the whole end of the button -->
/>
3. Custom Layout with TextView and ImageView (Full Control)
For maximum control, especially in complex layouts or when the above solutions don't fit your needs, consider using a LinearLayout or a ConstraintLayout with separate TextView and ImageView components. This approach allows for precise spacing control but requires more setup.
Example with LinearLayout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" <!-- For button-like feedback -->
android:orientation="horizontal"
android:padding="8dp" <!-- Button padding -->
>
<TextView
android:id="@+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Visit Website"
android:textAppearance="?attr/textAppearanceButton" <!-- Button text style -->
/>
<Space
android:layout_width="4dp" <!-- Custom spacing between text and icon -->
android:layout_height="wrap_content"
/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_open_in_new_black_24dp"
/>
</LinearLayout>
Choose the method that best fits your app's requirements:
app:iconPadding for a straightforward solution (Material 1.3.0+).
android:paddingEnd for a quick, albeit less precise, fix.
- Custom Layout for full control over spacing and appearance.