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:
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>