1

I am new to android development. I have a button for which i have provided the selector as below

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/menu_selected" android:state_selected="true"></item>
  <item android:drawable="@drawable/menu_pressed" android:state_pressed="true"></item>
  <item android:drawable="@drawable/list"></item>
</selector>

By this i am able to achieve the background image change on button Pressed and on button Selected, but i want to change the background color also along with the image. is this possible ? If it is then please guide me how to achieve the same.

Andy G
  • 19,232
  • 5
  • 47
  • 69
Rajesh Rs
  • 1,301
  • 5
  • 24
  • 48
  • 1
    http://stackoverflow.com/questions/17969925/how-to-highlight-a-button-when-is-pressed/17969979#17969979 – Raghunandan Sep 02 '13 at 10:48
  • 3
    If the image is covering the background of the button, what's the point of changing the background? – g00dy Sep 02 '13 at 10:48

2 Answers2

2
<item android:state_pressed="true"  android:drawable="@drawable/onclick_home"> 

  <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
        <stroke android:width="4dp" android:color="#94cd00" />

        <padding android:bottom="4dp" android:left="4dp" android:right="4dp" android:top="4dp" />

    </shape></item>

I have something like this to my imageview.It creates a green border outside my image view when it is pressed. see if it helps

Ritaban
  • 763
  • 4
  • 8
0
@Override
public boolean onTouch(View v, MotionEvent event)
{
    if(event.getAction() == MotionEvent.ACTION_UP)
    {
        //up event
        b.setBackgroundColor(Color.RED);
        return true;
    }
    if(event.getAction() == MotionEvent.ACTION_DOWN)
    {
        //down event
        b.setBackgroundColor(Color.GREEN);
        return true;
    }
    return false;
}
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
  • This is what the selector does actually, there's no point to do this in code. – Egor Sep 02 '13 at 10:56
  • This will only give the pressed state, not the selected state. – Ritaban Sep 02 '13 at 11:00
  • this also fails if I press down on the view, while still pressed down I slide my finger off the view, now I lift up. The color stays set. to the ACTION_DOWN color, it does not return to the ACTION_UP one – Sanketh Katta Jan 02 '15 at 02:43