1) Add a button view

We begin by placing a button in the activity layout file res/layout/activity_main.xml.

<?xml version=”1.0″ encoding=”utf-8″?> <RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent” android:paddingBottom=”@dimen/activity_vertical_margin” android:paddingLeft=”@dimen/activity_horizontal_margin” android:paddingRight=”@dimen/activity_horizontal_margin” android:paddingTop=”@dimen/activity_vertical_margin” tools:context=”com.mycompany.bubblebutton.MainActivity”>

<Button android:id=”@+id/button” android:layout_width=”92dp” android:layout_height=”92dp” android:onClick=”didTapButton” android:layout_centerHorizontal=”true” android:background=”#FFA400″ />

</RelativeLayout>

2) Create a scale animation

Next, we create an animation file res/anim/bounce.xml for scaling views.

  • Right click on the res folder.
  • Select New / Android resource file.
  • Write bounce as the name of the file
  • Choose the Animation resource type. The directory name field will change to anim.

Next, open the res/anim/bounce.xml file that was created for you and replace its content with the following code.

<?xml version=”1.0″ encoding=”utf-8″?> <set xmlns:android=”http://schemas.android.com/apk/res/android” >

<scale android:duration=”2000″ android:fromXScale=”0.3″ android:toXScale=”1.0″ android:fromYScale=”0.3″ android:toYScale=”1.0″ android:pivotX=”50%” android:pivotY=”50%” /> </set>

This code creates an animation that changes the size of a view from 30% to 100% during two seconds.

3) React to button tap

Now we add the code that animates the button on tap. Add the following method to your activity Java file.

If you run the app and tap the button it will animate smoothly from smaller to bigger size.