0

I have two buttons in layout,let suppose button A and button B and I want that when user touch any of two button,their background color should change for that moment.

code

``

<item android:state_hovered="true"
 android:drawable="@drawable/state_hovered"/>
<item android:state_pressed="true"
 android:drawable="@drawable/state_pressed"/>
<item android:drawable="@drawable/state_deafult" />

``

State_pressed working...but state_hovered is not working. So,please suggest a way to do so.

Thanks in advance.

Community
  • 1
  • 1
Gaurav
  • 13
  • 4

1 Answers1

0

Change color while button is pressed?

See an answer to this question, asked previosly in StackOverflow:

Create a my_button_background.xml file in drawable folder:

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

And use this in layout file:

android:background="@drawable/my_button_background"

And read more about Android's Color State. Basically, you can assign colors, drawables, shapes, etc. to different states of views, such as enabled, disabled, focused, clicked, etc.

Vitor Hugo Schwaab
  • 1,545
  • 20
  • 31