Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; SEOFriendlyImages has a deprecated constructor in /home/tutorig3/public_html/wp-content/plugins/seo-image/seo-friendly-images.class.php on line 2

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /home/tutorig3/public_html/wp-content/themes/mts_interactive/options/php-po/php-po.php on line 185
Android ViewSwitcher Using Kotlin With Example - Tutorialwing

Android ViewSwitcher Using Kotlin With Example

Hello Readers! In this post, we are going to learn about android viewSwitcher using kotlin in any android application. We will also learn about different attributes of android viewSwitcher that can be used to customise this widget.


Tutorialwing Kotlin ViewSwitcher Output Android ViewSwitcher Using Kotlin With Example

Tutorialwing Kotlin ViewSwitcher Output

Getting Started

Android ViewSwitcher can be defined as below –

Android ViewSwitcher is a viewAnimator that are used to switch between two views. You may use viewFactory to create views or add your custom view to create views.

Different Attributes of Android ViewSwitcher Widget

Some of the popular attributes of android viewSwitcher widget inherited from viewAnimator are –

Sr. XML Attributes Description
1 android:animateFirstView Defines whether to animate first view when the viewAnimation is first displayed
2 android:inAnimation Defines the animation to be used when view is shown
3 android:outAnimation Defines the animation to be used when view is hidden

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

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

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

Sr. XML Attributes Description
1 android:animateLayoutChanges Defines whether LayoutTransition should run whenever there is any changes in layout
2 android:animationCache Defines whether layout animations should create a drawing cache for their children
3 android:clipToPadding Defines whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero
4 android:layoutAnimation Defines the layout animation to use the first time the ViewGroup is laid out
5 android:layoutMode Defines the layout mode of this viewGroup

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

Sr. XML Attributes Description
1 android:alpha Defines the alpha of the view
2 android:background Defines the background of the view
3 android:backgroundTint Defines the tint to apply to background drawable
4 android:backgroundTintMode Defines blending mode to apply to background tint
5 android:clickable Defines whether view is clickable or not
6 android:elevation Defines z-depth of the view
7 android:focusable Defines whether this view can take focus or not
8 android:foreground Defines drawable to be used to show above this view

Example of Android ViewSwitcher Using Kotlin

At first, we will create android application. Then, we will use ViewSwitcher using kotlin in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ViewSwitcher. 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. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use viewSwitcher using kotlin in the application.

2. Modify values folder

Open res/values/strings.xml file. Then, add below code into it.

	<string name="app_name">ViewSwitcher</string>
	<string name="next">Next</string>
	<string name="no_image">No Image</string>

3. Download Drawable Resources Needed

You may need images, stored in res/drawable folder, that are needed in the application. These drawable images will be used by viewSwitcher in the application.

4. Use ViewSwitcher Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>







In activity_main.xml file, we have defined ViewSwitcher widget. Note that there are only two views (i.e. imageView) of viewSwitcher. However, only one view is shown at a time.

5. Access ViewSwitcher Widget in Kotlin file

Open src/main/java/com.tutorialwing.viewswitcher/MainActivity.kt file and add below code into it.

package com.tutorialwing.viewswitcher

import android.os.Bundle
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ViewSwitcher

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val viewSwitcher = findViewById<ViewSwitcher>(
        val `in` = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
        viewSwitcher.inAnimation = `in`

        val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
        viewSwitcher.outAnimation = out

        val button = findViewById<Button>(
        button?.setOnClickListener {

In file, we have accessed viewSwitcher widget. Then, we have set in and out animations in it. At last, we have set a click listener to show next view in the viewSwitcher whenever button is clicked.

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 main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.viewswitcher"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


When we run the program, we will get output as shown above.

That’s end of our tutorial on Android viewSwitcher using Kotlin.