I have a RelativeLayout
to which I have set a background from drawable
.I was able to change the background of RelativeLayout
to another when the RadioButton
is checked. But how do I give an animation to it when it changes?
Code: activity_main.xml:
<RelativeLayout
android:layout_width="80dp"
android:layout_height="50dp"
android:gravity="center"
android:background="@drawable/original"
android:id="@+id/rel1">
<RadioButton
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
original.xml(drawable):
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#ffffff">
</solid>
<corners
android:radius="50dp">
</corners>
</shape>
pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid
android:color="#2196F3">
</solid>
<corners
android:radius="50dp">
</corners>
</shape>
Java Class:
radio.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
relativeLayout.setBackgroundResource(R.drawable.pressed);
}
}
});