How to add image to button in android

I was wondering how I put the image on the button.

I tried using "android: icon =" @ drawable / search.png "and I add this image to the drawable-hdpi folder, but the image does not appear. I am developing an application for nexus 4, so I don’t know what size should be. Image which I am trying to add is the regular search icon used in nexus 4 contact manager.

The code I wrote so far.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/lbl_group_coworkers" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Coworkers" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" /> <TextView android:id="@+id/lbl_group_family" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Family" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/> <TextView android:id="@+id/lbl_group_friends" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Friends" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="0dip" android:layout_weight="1" android:orientation="vertical" > </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/Button1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.70" android:icon="@drawable/search.png" /> <Button android:id="@+id/Button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="addGroup" android:icon="@drawable/addgroup.png"/> <Button android:id="@+id/Button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.11" android:text="@string/Button3" /> </LinearLayout> </LinearLayout> 
+7
android android-layout
source share
4 answers
  <Button android:id="@+id/Button2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="addGroup" android:text="TEXT" android:background="@drawable/addgroup"/> 

To add background and image and text at the same time.

Just replace

 android:background="@drawable/addgroup" 

from

 android:background="@android:color/transparent" android:drawableTop="@drawable/addgroup" android:text="TEXT" 

You can also use attributes in the Button layout with properties that determine the source of the image that fits inside the button.

 android:drawableLeft android:drawableRight android:drawableBottom android:drawableTop 
+33
source share

use attribute

 android:background="@drawable/...." 
0
source share

You can set the button background using:

android:background="@drawable/..."

0
source share

Please create a folder with the ability to copy to res and create a buttonanimation.xml file in the folder with the picture

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="true" android:state_pressed="true"/> <item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="false" android:state_pressed="true"/> <item android:drawable="@drawable/add_incident_resetfocus" android:state_focused="true"/> <item android:drawable="@drawable/add_incident_reset" android:state_focused="false" android:state_pressed="false"/> </selector> 

And use this button in the layout button window.

  <Button android:id="@+id/reset_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/buttonanimation" /> 
0
source share

All Articles