Khurshid Yakubov
Khurshid Yakubov

Reputation: 69

How to put image on button in android studio

I have a button with xml background. And I want to put image on this button. It is not possible to put two backgrounds in one button. So, is there other way of putting image on my button while I have had a background yet? Below the code of my button:

<Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"
            android:layout_margin="15dp"
            android:background="@drawable/readmore_back"
             />  

Upvotes: 3

Views: 5103

Answers (3)

Liton
Liton

Reputation: 1

You can try this:

<ImageView
            android:id="@+id/battery"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:clickable="true"
            android:src="@drawable/laughing"/>

Upvotes: 0

Mr. Roshan
Mr. Roshan

Reputation: 1805

you may use

<ImageButton
 ..
 ..
 ..
 android:src="@drawable/image1"
/>

instead of

   <Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_row="0"
    android:layout_gravity="fill"
    android:layout_rowWeight="1"
    android:layout_columnWeight="1"
    android:layout_margin="15dp"
    android:background="@drawable/readmore_back"
     />  

Or

if you want to set the image to your existing Button then use

  • android:drawableTop,
  • android:drawableBottom,
  • android:drawableLeft,
  • android:drawableRight

Upvotes: 4

InsaneCat
InsaneCat

Reputation: 2161

Use ImageButton instead of Button

<ImageButton
    android:src="@drawable/my_image"
    android:text="@string/IMAGE"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

Upvotes: 0

Related Questions