How To Use An Image As A Button
Solution 1:
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"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_focused="true"android:state_pressed="false"android:drawable="@drawable/picture" /><itemandroid:state_focused="true"android:state_pressed="true"android:drawable="@drawable/picture_pressed" /><itemandroid:state_focused="false"android:state_pressed="true"android:drawable="@drawable/picture_pressed" /><itemandroid: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"
Solution 2:
Use ImageButton class.
Solution 3:
You can Use ImageButtons instead of regular buttons
The just set your background from your image in your XML
<ImageButtonandroid:background="@drawable/mypicture"/>
A nice tutorial is Here
Solution 4:
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(newOnClickListener() { publicvoidonClick(View v) { //TODO: your code here } });
Solution 5:
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 you.
Post a Comment for "How To Use An Image As A Button"