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?
-
Answer:
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"
Ian at Stack Overflow Visit the source
Other answers
marioosh
You can Use http://developer.android.com/reference/android/widget/ImageButton.html instead of regular buttons The just set your background from your image in your XML <ImageButton android:background="@drawable/mypicture"/> A nice tutorial is http://android-er.blogspot.com/2009/08/exercise-load-background-to-imagebutton.html
harper89
You need to write the android:background="@drawable/my_pic" to your xml file at the button. Where my_pic is your pic's name. Hope it helps.
user590898
Follow me, You can use many controls in android to solve this problem (Button, ImageView, ImageButton, or even TextView ... ). Just sets an image as its background and then implements OnClickListener to handle click event. Example, with ImageView, - in layout file: <ImageView android:id="@+id/button_login" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/button_login" /> in behind-code file: ImageView login; login = (ImageView) findViewById(R.id.button_login); login.setOnClickListener(new OnClickListener() { public void onClick(View v) { //TODO: your code here } });
Nguyen Minh Binh
Related Q & A:
- how to display .pgm image on a JFrame?Best solution by Stack Overflow
- How to upload the image to a server in Android PhoneGap?Best solution by Stack Overflow
- How to add an image to a button?Best solution by Stack Overflow
- How can I auto fresh when a button is clicked?Best solution by Stack Overflow
- How to make an image have a different color?Best solution by superuser.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.