Define Motion For Buttons In Android Using Android Studio

Introduction

 
In this article, we are going to see how to create a motion button in the Android app using the Android Studio. In a user interface the motion button is a unique thing that will entice users to click it. We can define the different levels of motions to the button.
 
Step 1
 
Create a new project in Android Studio.
 
 
Give a name to the project and click "Next".
 
 
Select the "Phone and Tablet" and click "Next".
 
 
Select an empty activity and click "Next".
 
 
At last, give the activity a name and click on "Finish".
 
 
Step 2
 
Setup the Gradle by just locating the Gradle Scripts>>Build. Gradle
 
 
And type the following dependency in your app's build.gradle.
 
 
Code copy is here.
  1. implementation 'com.appolica:flubber:1.0.1'  
Step 3
 
Next, go to app >> res >> layout >> activity_main.xml. Select activity page.
 
 
And just type the code as follows.
 
 
The code copy is here.
  1. <Button  
  2.     android:id="@+id/button"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_centerHorizontal="true"  
  6.     android:layout_centerVertical="true"  
  7.     android:text="Subscribe Now" />  
Step 4
 
Next, go to app >> java>>Mainactivity.java. Select Mainactivity page:
 
 
And just type the code as follows:
 
 
Code copy is here.
  1. Button button = (Button) findViewById(R.id.button);  
  2.        button.setOnClickListener(new View.OnClickListener() {  
  3.            @Override  
  4.            public void onClick(View view) {  
  5.                Flubber.with()  
  6.                        .animation(Flubber.AnimationPreset.ROTATION)  
  7.                        .repeatCount(1)  
  8.                        .duration(1000)  
  9.                        .createFor(view)  
  10.                        .start();  
  11.            }  
  12.        });  
Step 5
 
After step 4, Sync all the dependency gradles and Mainactivity.java resource files by clicking the Sync button on the top right corner of the Gradle page.
 
 
Step 6
 
Verify the preview.
->After the code is applied, the preview will appear like this.
 
 
Step 7
 
Next, go to Android Studio and deploy the application. Select an Emulator or your Android mobile with USB debugging enabled. Give it a few seconds to make installations and set permissions.
 
 
 
Explanation of source code
 
The source code provided in this article is just the dependencies of motions and the code used in activity_main.xml will make the button move and define its attributes.
 

Summary

 
In this article, we created an app named MotionButton, then we inserted a new Button and we learned how to give motion to that button and finally, we have deployed it as an output.


Similar Articles