I am developing an Android app that has a button which spawns a browser. To make this clear to the user, I want to add an 'external link' icon after the button text. Which android:drawable resource should I use?
Assistant
To add an external link icon (🌐) to your Android button, use the Material Design "open in new" icon (ic_open_in_new). Here's how to implement it:
1. Use Material Design Icon (Recommended)
Add the Material Components dependency in your build.gradle (if not already included):
Then, set the icon in your Button/MaterialButton XML:
xml
User
How do I adjust the spacing between the button text and the Material icon when using app:iconGravity="textEnd", especially when the button has dynamic width and the icon appears too close to the text edge?
Assistant
To adjust the spacing between the button text and the Material icon when using app:iconGravity="textEnd", you can use app:iconPadding to control the gap. Here's how to fix the spacing issue:
User
How do I maintain consistent icon spacing in a MaterialButton when the text length varies dynamically and the button's width is match_parent, without causing the icon to overflow or misalign in RTL layouts?
Assistant
To maintain consistent spacing between dynamic text and the icon in a with while ensuring RTL compatibility, use a of padding, constraints, and RTL-aware attributes. Here's the optimized solution:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
<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" <!-- Icon -->
app:iconGravity="textEnd" <!-- Place icon after text -->
app:iconTint="@color/your_color" <!-- Optional: Set icon color -->
/>
2. Android Default Icon (Legacy)
If you prefer the system default, use ic_menu_share (not strictly an external link icon, but commonly understood):
Download the Material icon from Material Icons (search for "open in new").
Add it to your project via File > New > Vector Asset in Android Studio.
Use it in XML:
xml
android:drawableEnd="@drawable/ic_external_link"
Key Attributes:
drawableEnd: Places the icon to the right of the text (standard Button/TextView).
app:icon + app:iconGravity: More control with MaterialButton.
Choose the method that aligns with your app’s design system!
1. Use iconPadding (Simplest Solution)
Add app:iconPadding to your MaterialButton XML to create space between the text and icon:
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"
app:iconGravity="textEnd"
app:iconPadding="8dp" <!-- Adjust this value (e.g., 4dp, 8dp, 12dp) -->
/>
2. Add Margin to the Icon Itself
If you need more control, wrap the icon in a <layer-list> to add intrinsic padding:
Create a custom drawable (e.g., ic_open_in_new_padded.xml):
xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_open_in_new"
android:left="8dp" <!-- Adds padding to the left of the icon -->
/>
</layer-list>
app:iconPadding: Directly controls the space between the text and icon.
Custom drawables: Add fixed spacing to the icon asset itself.
Button padding: Ensures the entire button isn’t too tight, especially with dynamic widths.
Bonus: Programmatic Adjustment
If you need dynamic spacing (e.g., for different screen sizes):
kotlin
val button = findViewById<MaterialButton>(R.id.your_button)
button.iconPadding = resources.getDimensionPixelSize(R.dimen.icon_padding) // e.g., 8dp
This ensures the icon is visually separated from the text edge while maintaining responsiveness. Adjust values based on your design system! 🎨
MaterialButton
width="match_parent"
combined approach
1. XML Configuration
Use iconPadding for text-icon spacing and symmetrical padding to prevent overflow:
xml
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dynamic_text"
android:paddingStart="24dp" <!-- Ensures space from left (LTR) / right (RTL) edge -->
android:paddingEnd="24dp" <!-- Mirror for RTL -->
app:icon="@drawable/ic_open_in_new"
app:iconGravity="textEnd" <!-- Auto-flips to "textStart" in RTL -->
app:iconPadding="8dp" <!-- Space between text and icon -->
app:iconTint="@color/icon_color"
/>
2. Constrain Minimum Width (Optional)
Prevent cramped layouts when text is short:
xml
android:minWidth="120dp" <!-- Adjust based on your design -->
3. Custom Drawable with Intrinsic Padding (Advanced)
For precise control, add padding directly to the icon asset:
Create ic_open_in_new_padded.xml:
xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/ic_open_in_new"
android:left="8dp" <!-- Padding for LTR -->
android:right="8dp" <!-- Padding for RTL -->
/>
</layer-list>
Use the padded icon:
xml
app:icon="@drawable/ic_open_in_new_padded"
4. RTL Testing
Ensure proper mirroring in RTL layouts:
xml
<!-- In AndroidManifest.xml -->
<application
...
android:supportsRtl="true">
Test with:
Long/short text variations.
Forced RTL in developer options.
How It Works
iconPadding: Maintains fixed space between text and icon.
Symmetrical paddingStart/End: Prevents the icon from touching the button’s edge in both LTR/RTL.
RTL-aware iconGravity: Automatically flips the icon position based on layout direction.