Rajesh Rs
Rajesh Rs

Reputation: 1391

Changing button background color on pressed

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.

Upvotes: 1

Views: 4545

Answers (2)

Ritaban
Ritaban

Reputation: 773

<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

Upvotes: 2

Naveen Tamrakar
Naveen Tamrakar

Reputation: 3339

@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;
}

Upvotes: 0

Related Questions