Android ViewAnimator Using Kotlin With Example

In this article, we will learn about android ViewAnimator using Kotlin. We will go through various example that demonstrates how to use different attributes of ViewAnimator. For example,

In this article, we will get answer to questions like –

  • What is ViewAnimator?
  • Why should we consider ViewAnimator while designing ui for any app?
  • What are possibilities using ViewAnimator while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin ViewAnimator Output Android ViewAnimator Using Kotlin With Example

Tutorialwing Kotlin ViewAnimator Output

Getting Started

We can define android ViewAnimator widget as below –

ViewAnimator is a widget that are used to switch between views using animations.

Now, how do we use ViewAnimator in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ViewAnimator. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ViewAnimator Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ViewAnimator in Kotlin

Follow steps below to use ViewAnimator in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ViewAnimator</string>
        <string name="next">Next</string>
        <string name="prev">Previous</string>
        <string name="item_post_1">Text at position 1</string>
        <string name="item_post_2">Button at position 2</string>
        <string name="item_post_4">Text at position 4</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp">
    
            <Button
                android:id="@+id/buttonPrev"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/prev" />
    
            <Button
                android:id="@+id/buttonNext"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/next" />
    
        </LinearLayout>
    
        <ViewAnimator
            android:id="@+id/viewAnimator"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_1"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp" />
    
            <Button
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_2" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/app_name"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_4"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp" />
    
        </ViewAnimator>
    
    </LinearLayout>
    

    In activity_main.xml file, we have two buttons and ViewAnimator. ViewAnimator contains four child views(TextView, Button, ImageView and TextView). At a time, only one view is shown in viewAnimator. You can switch between views, using viewAnimator, with animation. Now, we will access these widgets in kotlin file to perform some actions on it.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="horizontal"
            android:padding="10dp">
    
            <Button
                android:id="@+id/buttonPrev"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/prev" />
    
            <Button
                android:id="@+id/buttonNext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:text="@string/next" />
    
        </LinearLayout>
    
        <ViewAnimator
            android:id="@+id/viewAnimator"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_1"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_2" />
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@string/app_name"
                android:src="@mipmap/ic_launcher" />
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="@string/item_post_4"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="20sp" />
    
        </ViewAnimator>
    
    </LinearLayout>
    

    We have accessed viewAnimator using kotlin file (i.e. MainActivity.kt file). Then, we have set in and out animations that are applied when current view is replaced by either previous view or next view. Then, we have set click listener on buttons (Prev and Next buttons) that shows previous/next view in viewAnimator respectively.

Now, run the application. We will get output as below –

Tutorialwing Kotlin ViewAnimator Output Android ViewAnimator Using Kotlin With Example

Tutorialwing Kotlin ViewAnimator Output

Different Attributes of ViewAnimator in XML

Now, we will see how to use different attributes of Android ViewAnimator using Kotlin to customise it –

Set Id of ViewAnimator

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ViewAnimator using android:id attribute like below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        />

Here, we have set id of ViewAnimator as viewAnimator_ID using android:id=”” attribute. So, if we need to reference this ViewAnimator, we need to use this id – viewAnimator_ID.
Learn to Set ID of ViewAnimator Dynamically

Set Width of ViewAnimator

We use android:layout_width=”” attribute to set width of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of ViewAnimator Dynamically

Set Height of ViewAnimator

We use android:layout_height=”” attribute to set height of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of ViewAnimator Dynamically

Set Padding of ViewAnimator

We use android:padding=”” attribute to set padding of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ViewAnimator using android:padding=”” attribute.
Learn to Set Padding of ViewAnimator Dynamically

Set Margin of ViewAnimator

We use android:layout_margin=”” attribute to set margin of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ViewAnimator using android:layout_margin=”” attribute.
Learn to Set Margin of ViewAnimator Dynamically

Set Background of ViewAnimator

We use android:background=”” attribute to set background of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ViewAnimator using android:background=”” attribute.
Learn to Set Background of ViewAnimator Dynamically

Set Visibility of ViewAnimator

We use android:visibility=”” attribute to set visibility of ViewAnimator.
We can do it as below –

    <ViewAnimator
        android:id="@+id/viewAnimator_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ViewAnimator using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of ViewAnimator Dynamically

Till now, we have see how to use android ViewAnimator using Kotlin. We have also gone through different attributes of ViewAnimator to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android ViewAnimator Widget

Below are the various attributes that are used to customise android ViewAnimator Widget. However, you can check the complete list of attributes of ViewAnimator in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of android ViewAnimator widget are –

Sr. XML Attributes Description
1 android:animateFirstView Specifies whether to animate current view when the viewAnimation is first displayed
2 android:inAnimation Identifier for the animation to use when a view is shown
3 android:outAnimation Identifier for the animation to use when a view is hidden

Some of the popular attributes of android ViewAnimator inherited from FrameLayout are –

Sr. XML Attributes Description
1 android:foregroundGravity Specifies the gravity to apply for foreground drawable
2 android:measureAllChildren Specifies whether to measure all children or just those in VISIBLE or INVISIBLE state when measuring

Some of the popular attributes of android ViewAnimator inherited from ViewGroup are –

Sr. XML Attributes Description
1 android:splitMotionEvents Specifies this viewGroup should split MotionEvents to separate child views during touch event dispatch
2 android:persistentDrawingCache Specifies persistence of the drawing cache
3 android:layoutMode Specifies layout mode of this viewGroup
4 android:layoutAnimation Specifies the layout animation to be used when this viewGroup is laid out for the first time
5 android:clipToPadding Specifies whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero.

Some of the popular attributes of android ViewAnimator inherited from View are –

Sr. XML Attributes Description
1 android:background Specifies drawable of the background
2 android:backgroundTint Specifies tint to apply to the background
3 android:backgroundTintMode Specifies blending mode to be used to apply tint to the background
4 android:clickable Specifies whether this view is clickable or not
5 android:id Specifies unique id of the view
6 android:onClick Specifies what to do when this view is clicked
7 android:padding Specifies padding of the view
8 android:visibility Specifies visibility(VISIBLE, INVISIBLE or GONE) of the view

We have seen different attributes of ViewAnimator and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ViewAnimator, how can we use android ViewAnimator using Kotlin ? etc. We also went through different attributes of android ViewAnimator.

Leave a Reply