Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.Hello Readers! In this post, we are going to learn about how to use android viewAnimator programmatically in kotlin in the application. We will also learn how to add viewAnimator to linearLayout programmatically in android application.
Output

Tutorialwing Kotlin Dynamic ViewAnimator Output
Getting Started
At first, we will create android project. Then, we will create and use viewAnimator programmatically in kotlin file.
1. Creating New Project in Kotlin
Follow the steps below to create new project. Please ignore the steps if you have already created a new project.
Step | Description |
---|---|
1. | Open Android Studio. |
2. | Go to File => New => New Project. Write application name as DynamicViewAnimator. Then, check Include Kotlin Support and click next button. |
3. | Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button |
4. | Then, select Empty Activity => click next => click finish. |
5. | At this point, You will get a newly created android project successfully. |
Now, we will modify xml and kotlin file to use viewAnimator dynamically.
2. Modify Values Folder
Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">DynamicViewAnimator</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 positon 4</string> </resources>
3. Modify Layout Folder
Open res/layout/activity_main.xml file. Then, add below code into 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" 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:text="@string/next"/> </LinearLayout> <LinearLayout android:id="@+id/rootContainer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> </LinearLayout>
In activity_main.xml file, we have defined buttons a linearLayout, with id rootContainer, that will act as container for the viewAnimator created programmatically in the application.
4. Create Android ViewAnimator Programmatically / Dynamically in Kotlin
Open app/src/main/java/com.tutorialwing.dynamicviewanimator/MainActivity.kt file. Then, add below code into it.
package com.tutorialwing.dynamicviewanimator import android.graphics.Color import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.ViewGroup import android.view.animation.AnimationUtils import android.widget.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val viewAnimator = ViewAnimator(this) val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) layoutParams.setMargins(30, 30, 30, 30) viewAnimator.layoutParams = layoutParams val animationIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left) val animationOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right) viewAnimator.inAnimation = animationIn viewAnimator.outAnimation = animationOut val textView = TextView(this) val textParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) textView.layoutParams = textParams textView.text = getString(R.string.item_post_1) textView.setTextColor(Color.RED) viewAnimator.addView(textView) val button = Button(this) val buttonParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) button.layoutParams = buttonParams button.text = getString(R.string.item_post_2) button.setTextColor(Color.RED) viewAnimator.addView(button) val imageView = ImageView(this) val imageParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) imageView.layoutParams = imageParams imageView.setImageResource(R.mipmap.ic_launcher) viewAnimator.addView(imageView) val textView2 = TextView(this) val textParams2 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) textView2.layoutParams = textParams2 textView2.text = getString(R.string.item_post_4) textView2.setTextColor(Color.RED) viewAnimator.addView(textView2) val linearLayout = findViewById<LinearLayout>(R.id.rootContainer) linearLayout.addView(viewAnimator) val buttonPrev = findViewById<Button>(R.id.buttonPrev) buttonPrev.setOnClickListener({ viewAnimator.showPrevious() }) val buttonNext = findViewById<Button>(R.id.buttonNext) buttonNext.setOnClickListener({ viewAnimator.showNext() }) } }
We have created viewAnimator programmatically in kotlin file( i.e. in MainActivity.kt file) in the application. Then, we have set layout params, margins etc. in it. Then, we have created child views for viewAnimator. They are TextView, Button, ImageView and TextView respectively. Each one of them have been added to viewAnimator. Then, we have added this viewAnimator in linearLayout having id rootContainer. Then, we have added click listener on prev and next buttons to show previous and next view in viewAnimator respectively.
Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.
AndroidManifest.xml file
Code inside src/main/AndroidManifest.xml file is –
<?xml version="1.0" encoding="utf-8"?> <manifest package="com.tutorialwing.dynamicviewanimator" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the program, we will get output as shown above.
That’s end of our tutorial on Creating Android ViewAnimator Programmatically in Kotlin.