View Shaker is an animation in which, the UI of screen vibrates for a limited period of time. This can be implemented on the whole layout or some particular widget. It is a very common effect that developers use, especially to show incorrect credentials.
View Shaker helps us to animate the widgets. Various effects can be added to it.
Effects
- Attension
- Special
- Hinge, RollIn, RollOut, Landing, TakingOff, DropOut
- Bounce
-
BounceIn, BounceInDown, BounceInLeft, BounceInRight, BounceInUp
- Fade
-
FadeIn, FadeInUp, FadeInDown, FadeInLeft, FadeInRight
FadeOut, FadeOutDown, FadeOutLeft, FadeOutRight, FadeOutUp - Flip
-
FlipInX, FlipOutX, FlipOutY
- Rotate
-
RotateIn, RotateInDownLeft, RotateInDownRight, RotateInUpLeft, RotateInUpRight
RotateOut, RotateOutDownLeft, RotateOutDownRight, RotateOutUpLeft, RotateOutUpRight - Slide
-
SlideInLeft, SlideInRight, SlideInUp, SlideInDown
SlideOutLeft, SlideOutRight, SlideOutUp, SlideOutDown - Zoom
-
ZoomIn, ZoomInDown, ZoomInLeft, ZoomInRight, ZoomInUp
ZoomOut, ZoomOutDown, ZoomOutLeft, ZoomOutRight, ZoomOutUp
- Flash, Pulse, RubberBand, Shake, Swing, Wobble, Bounce, Tada, StandUp, Wave
In this article we will see an example of adding such a effect in an app.
In this example, a user wants to log into GeeksforGeeks portal. The user enters the wrong password and then clicks on the login button. Then we can animate our view making the app more responsive, using View Shaker.
Approach
-
Add the support Library in build.gradle file and add dependency in the dependencies section. This library has various animations effect described above. It helps in making our application more responsive and dynamic.
dependencies {implementation 'com.daimajia.easing:library:2.0@aar'implementation 'com.daimajia.androidanimations:library:2.3@aar'}chevron_rightfilter_none - Now add the following code in the activity_main.xml file. This code add one textview, two edittexts and a button on activity_main.
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><TextViewandroid:textStyle="bold"android:textSize="24sp"android:textColor="#219806"android:layout_margin="15dp"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="GeeksForGeeks"/><EditTextandroid:id="@+id/editText1"android:layout_margin="15dp"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Enter ID"/><EditTextandroid:id="@+id/editText2"android:layout_margin="15dp"android:inputType="textPassword"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Enter Password"/><Buttonandroid:id="@+id/button"android:layout_margin="25dp"android:layout_gravity="center"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Log In"/></LinearLayout>chevron_rightfilter_none - Now add the following code in the MainActivity.java file. onClickListener is added to Log In button which adds the Shake effect on both the edittext.The different functions like duration sets the duration, repeat set the number of times the effect should be repeated and playOn sets the effect on a particular widget.
MainActivity.java
packageorg.geeksforgeeks.gfgviewshaker;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.EditText;importcom.daimajia.androidanimations.library.Techniques;importcom.daimajia.androidanimations.library.YoYo;publicclassMainActivityextendsAppCompatActivity {Button login;EditText id, password;@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);login = findViewById(R.id.button);id = findViewById(R.id.editText1);password = findViewById(R.id.editText2);login.setOnClickListener(newView.OnClickListener() {@OverridepublicvoidonClick(View v){YoYo.with(Techniques.Shake).duration(500).repeat(2).playOn(id);YoYo.with(Techniques.Shake).duration(500).repeat(2).playOn(password);}});}}chevron_rightfilter_none
Output:
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to implement Swipe Down to Refresh in Android using Android Studio
- Android | Creating a Calendar View app
- How to create a Circular image view in Android without using any library?
- How to add KenBurns View in Android?
- View Recycling in Android with ListView
- Elastic View in Android
- How to implement Picture in Picture (PIP) in Android?
- How to implement Options Menu in Android
- How to implement Dark (Night) mode in Android app
- How to implement a TabLayout in Android using ViewPager and Fragments
- Implement Zoom In or Zoom Out in Android
- Implement customized TimePicker in Android using SnapTimePicker
- Implement reverse AnimatedVectorDrawables using MorphView in Android
- How to Implement Press Back Again to Exit in Android?
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

