skyie
skyie

Reputation: 1

Create a Button Animation in Android Studio

I wanted to create a button that changes it's colour when clicked on it . Also the button should pop out a little and then pop back in or bounce . I already checked out the post related to this but I'm sorry to say that it didn't work out for me. Also it will be really helpful if you mention the entire part of the code and not the necessary parts. Thanks a lot in Advance :) I can't post the image of the code I typed cos of less reputation . So sorry about that.

Upvotes: 0

Views: 2918

Answers (1)

Rizwan Atta
Rizwan Atta

Reputation: 3295

for just button color change only you can always use Selector drawable as a back ground for example like this!

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="20dp"></corners>

        </shape>

    </item>

<item android:state_pressed="true">
    <shape android:shape="rectangle">
        <solid android:color="#000"></solid>
        <corners android:radius="10dp"></corners>

    </shape>



</item>

//--------------------------------------------------------------------------//

for animation like to raise up you can give a feel of size animation or for free form moving your should use Translate anim

here is the way!

1: put a new res folder called anim and right click on it which will let you make the anim resource and there you make one like "translate_anim.xml" and put this in it

In order to change position of object use tag. It uses fromXDelta, fromYDelta for X-direction and toXDelta, toYDelta attributes for Y-direction. move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

And then in java you have to go and set up the animation using this

Animation translater = AnimUtils.loadAnimation(getApplicationContext,R.anim.translate_anim);

Button btnMoving = findViewbyId(R.id.btn_moving);
btnMoving.startAnimation(btnMoving);

here is the resource anim file for scale UP.

slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />

</set>

Upvotes: 1

Related Questions