Android View Shaker in Kotlin
Last Updated :
02 Jun, 2022
View Shaker is an android animation in which the UI of the screen vibrates for a specific interval of time. We can implement this View shaker to the specific views of the application. View Shaker provides different animation effects which we can add to our views such as bounce, fade, float, and others. In this article, we will take a look at implementing this view shaker within our Android application using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.
Note: To implement View Shaker in Android application in Java. Check out the following article: View Shaker in Android using Java
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Adding dependency in build.gradle
Navigate to Gradle Scripts > build.gradle and add the below dependency in the dependencies section.
implementation 'com.daimajia.easing:library:2.0@aar'
implementation 'com.daimajia.androidanimations:library:2.3@aar'
After adding this dependency simply sync your project to install it.
Step 3: Adding images to the drawable folder
Copy your image. Navigate to app > res > drawable folder and paste your image into the drawable folder.
Step 4: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.
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"
tools:context=".MainActivity">
<!--on below line we are creating a text view-->
<TextView
android:id="@+id/idTVData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:gravity="center"
android:padding="6dp"
android:text="Geeks for Geeks"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="20sp"
android:textStyle="bold" />
<!--on below line we are creating our image view-->
<ImageView
android:id="@+id/idIVimage"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_below="@id/idTVData"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/android" />
<!--on below line we are creating one more text view-->
<TextView
android:id="@+id/idTVAndroid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idIVimage"
android:layout_marginTop="30dp"
android:gravity="center"
android:padding="8dp"
android:text="Android development"
android:textAlignment="center"
android:textColor="@color/purple_200"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.gtappdevelopers.kotlingfgproject
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.daimajia.androidanimations.library.Techniques
import com.daimajia.androidanimations.library.YoYo
class MainActivity : AppCompatActivity() {
// on below line we are creating
// variables for text view and image view
lateinit var androidTV: TextView
lateinit var dataTV: TextView
lateinit var androidIV: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// initializing variables of list view with their ids.
androidIV = findViewById(R.id.idIVimage)
androidTV = findViewById(R.id.idTVAndroid)
dataTV = findViewById(R.id.idTVData)
// on below line we are adding on
// click listener for our image view.
androidIV.setOnClickListener {
// on the below line we are adding a shake
// animation for our image view.
YoYo.with(Techniques.Shake)
// on below line we are setting
// duration for our animation.
.duration(500)
// on below line we are setting repeat
// duration for our animation
.repeat(2)
// on below line we are specifying view
// on which we have to add this animation
.playOn(androidIV)
// on below line we are adding shake
// animation for our image view.
YoYo.with(Techniques.Shake)
// on below line we are setting
// duration for our animation.
.duration(500)
// on below line we are setting
// repeat duration for our animation
.repeat(2)
// on below line we are specifying view
// on which we have to add this animation
.playOn(androidTV)
// on below line we are adding shake
// animation for our image view.
YoYo.with(Techniques.Shake)
// on below line we are setting
// duration for our animation.
.duration(500)
// on below line we are setting
// repeat duration for our animation
.repeat(2)
// on below line we are specifying view
// on which we have to add this animation
.playOn(dataTV)
}
}
}
Now run your application to see the output of it.
Output:
Similar Reads
Android WebView in Kotlin
WebView is a view that is used to display web pages inside the app. It is used to turn the app into a web application. In this article let's display the https://www.geeksforgeeks.org/ inside the Android Application using Kotlin. Note: To implement Android WebView in Java please refer How to use WebV
2 min read
Android ListView in Kotlin
ListView in Android is a ViewGroup which is used to display a scrollable list of items arranged in multiple rows. It is attached to an adapter which dynamically inserts the items into the list. The main purpose of the adapter is to retrieve data from an array or a database and efficiently push every
3 min read
Android RecyclerView in Kotlin
In this article, you will know how to implement RecyclerView in Android using Kotlin . Before moving further let us know about RecyclerView. A RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ab
4 min read
SearchView in Android with Kotlin
SearchView is a widget in android which provides a search interface with the help of which users can be able to make searches within the given list of data. In this search view, the user has to specify the search query. According to the search, query results will be populated within the listview. In
4 min read
Android ViewPager in Kotlin
ViewPager adds a sleek, swipeable interface that lets users effortlessly navigate between pages, just like flipping through a book. This feature is common in social media apps; for instance, WhatsApp greets you with three intuitive tabs: chats, status, and calls. This makes the app look cool. You've
5 min read
How to implement View Shaker in Android
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.
3 min read
Android Toast in Kotlin
A Toast is a short alert message shown on the Android screen for a short interval of time. Android Toast is a short popup notification which is used to display information when we perform any operation in our app. In this tutorial, we shall not just limit ourselves by creating a lame toast but also
3 min read
Android Material Tabs in Kotlin
In Android, TabLayout is a new element introduced in the Design Support library. It provides a horizontal layout to display tabs on the screen. We can display more screens on a single screen using tabs. We can quickly swipe between the tabs. TabLayout is basically ViewClass required to be added into
5 min read
Shared ViewModel in Android
In Android development, ViewModel serves as a means to facilitate data sharing among different fragments or activities. By utilizing the same ViewModel across multiple fragments, each fragment gains access to the shared data and functionality encapsulated within the ViewModel. This approach offers a
4 min read
ViewFlipper in Android with Kotlin
View Flipper is a UI component in Android which is used to flip the view within the screen in the android application. The View Flipper class is an extension of ViewAnimator class. With the help of this, we can simply flip the views. In this article, we will take a look at How we can use ViewFlipper
4 min read