How can I add an image to a button, rather than text?
you can use ImageView as Button. Create an ImageView and set clickable true after in write imageView.setOnClickListener for ImageView.
<ImageView
android:clickable="true"
android:focusable="true"`
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
and in Activity's oncreate:
imageView.setOnClickListener(...
The new MaterialButton
from the Material Components can include an icon:
<com.google.android.material.button.MaterialButton
android:id="@+id/material_icon_button"
style="@style/Widget.MaterialComponents.Button.TextButton.Icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/icon_button_label_enabled"
app:icon="@drawable/icon_24px"/>
You can also customize some icon properties like iconSize
and iconGravity
.
Reference here.
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="News Feed"
android:icon="@drawable/newsfeed" />
newsfeed is image in the drawable folder