Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use an image as a button

this should be an easy one and I really thought that what I was doing was correct but apparently it isn't. I have an image that I want to use as a button, I have the button created and working. But now I just want this image to show up instead of the standatd android "button".

Anyone know how to do this?

like image 363
Ian Avatar asked Jul 21 '11 14:07

Ian


People also ask

Can you use an image as a button?

The image buttons in the HTML document can be created by using the type attribute of an <input> element. Image buttons also perform the same function as submit buttons, but the only difference between them is that you can keep the image of your choice as a button.

How do I link an image to a button in HTML?

Complete HTML/CSS Course 2022 To use image as a link in HTML, use the <img> tag as well as the <a> tag with the href attribute. The <img> tag is for using an image in a web page and the <a> tag is for adding a link. Under the image tag src attribute, add the URL of the image.


1 Answers

Make a use of <selector

Create in your drawable-hdpi folder xml file called button_image.xml

and add the following lines:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/picture" /> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/picture_pressed" /> 
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/picture_pressed" /> 
    <item android:drawable="@drawable/picture" /> 
</selector>

and then in your main xml where you declare your button add this line

android:background="@drawable/button_image"

And then you get effect of reall button. Note that you need two pictures to get effect of button being clicked.

If you dont want to use the selector and use just picture instead then just

android:background="@drawable/myimage"

like image 167
Nikola Despotoski Avatar answered Sep 18 '22 15:09

Nikola Despotoski