Button - Change background color on click
use this selector & put it in drawable folder & set button background to this selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_pressed="true"/>
<item android:drawable="@color/AliceBlue" android:state_focused="true"/>
<item android:drawable="@color/Azure"/>
</selector>
OR You can use Color instead of Background. hope it helps.
I suggest you this selector.
Just create a simple selector.xml file in drawable folder and then add the selector to your button as android:background="@drawable/selector"
or by code like this: yourButton.setBackground(getResources().getDrawable(R.drawable.selector));
selector.xml
:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="@android:color/holo_blue_light" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="@android:color/holo_green_dark" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="@android:color/holo_purple" />
<item
android:drawable="@drawable/yourdrawable" />
</selector>
The first item is for pressed
, the second for focused
and the last is for selected
state.
Create a shape named button_pressed.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/blue" />
<stroke
android:width="4dp"
android:color="@color/blue" />
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
</shape>
Suppose, you have two buttons whose IDs are R.id.btn
and R.id.btn1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 1" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="@drawable/button_pressed"
android:onClick="onClick"
android:text="Press Me 2" />
</LinearLayout>
Write the onClick()
method which will allow you to retain the changed color until another button is pressed.
Button button;
public void onClick(View v) {
Drawable dr = getResources().getDrawable(R.drawable.button_pressed);
dr.setColorFilter(Color.parseColor("#FF0000"), PorterDuff.Mode.SRC_ATOP);
switch (v.getId()) {
case R.id.btn:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
case R.id.btn2:
if (button == null) {
button = (Button) findViewById(v.getId());
} else {
button.setBackgroundResource(R.drawable.button_pressed);
button = (Button) findViewById(v.getId());
}
button.setBackgroundDrawable(dr);
break;
default:
break;
}
}
I think, now you will get What you wanted to do.