0

I have very simple questions, How to add an image in custom button?

   <com.example.KeyButton 
                android:id="@+id/custombutton" 
                android:layout_gravity="center_vertical" 
                android:layout_height="fill_parent"     />
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
vik
  • 33
  • 1
  • 8

5 Answers5

1

Add this Tag android:background="@drawable/your_image"

<com.example.KeyButton 
            android:id="@+id/custombutton" 
            android:layout_gravity="center_vertical" 
            android:layout_height="fill_parent"
            android:background="@drawable/your_image" />
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
1

You have to add android:backgroud to add image.

<com.example.KeyButton 
android:id="@+id/custombutton" 
 android:layout_gravity="center_vertical" 
 android:layout_height="fill_parent"  
android:background"@drawable/image"   /> //here will be your image.
RobinHood
  • 10,897
  • 4
  • 48
  • 97
0
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/yourImageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/your_image" />

</LinearLayout>

And put "your_image" in the drawable folder.

michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
0

put image of button in drawble folder and put this code in your xml file....

       <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        <Button
            android:id="@+id/btn_your_btn_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/set_alarm_button" />
    </LinearLayout>

hope this will help.

Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30
0

you can use programmatically the methods :

setBackground(Color c); and
setBackground(Drawable d);
setBackgroundResource(int resId);

otherwise, you can use the xml way like stated above

karvoynistas
  • 1,295
  • 1
  • 14
  • 30