开发者

how to change button color when it is clicked in android

By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA

I used setBackgrou开发者_C百科ndColor(Color.BLUE); But here the color is applied and not gone...


You need to use a selector as your background resource :

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

This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.

You need to put this XML into your drawables directory and use it as a background for your button.

For another solution refer : Standard Android Button with a different color


i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.

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

above is the content of the xml file,which must be assigned to the background of the respective button android:background="@drawable/drawable_button

hope this might be helpful for you

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜