Android ViewSwitcher Using Kotlin With Example

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

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

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

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

Output

Tutorialwing Kotlin ViewSwitcher Output Android ViewSwitcher Using Kotlin With Example

Tutorialwing Kotlin ViewSwitcher Output

Getting Started

We can define android ViewSwitcher widget 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.

Now, how do we use ViewSwitcher 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 ViewSwitcher. 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 ViewSwitcher 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 ViewSwitcher in Kotlin

Follow steps below to use ViewSwitcher in newly created project –

  • Download Drawable Resources Needed

    We 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.

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ViewSwitcher</string>
        <string name="next">Next</string>
        <string name="no_image">No Image</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">
    
        <Button
            android:id="@+id/btnNext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="20dp"
            android:text="@string/next"/>
    
        <ViewSwitcher
            android:id="@+id/viewSwitcher"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/image1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/no_image"
                android:src="@drawable/pomegranate"/>
    
            <ImageView
                android:id="@+id/image2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:contentDescription="@string/no_image"
                android:src="@drawable/zespri_kiwi"/>
    
        </ViewSwitcher>
    
    </LinearLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.viewswitcher
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.animation.AnimationUtils
    import com.tutorialwing.viewswitcher.databinding.ActivityMainBinding
    
    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)
    
    		setupViewSwitcher()
    	}
    
    	private fun setupViewSwitcher() {
    		val viewSwitcher = binding.viewSwitcher
    		val inAnim = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left)
    		viewSwitcher.inAnimation = inAnim
    
    		val out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right)
    		viewSwitcher.outAnimation = out
    
    		binding.btnNext.setOnClickListener {
    			viewSwitcher.showNext()
    		}
    	}
    }
    

    In MainActivity.kt 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.

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

Tutorialwing Kotlin ViewSwitcher Output Android ViewSwitcher Using Kotlin With Example

Tutorialwing Kotlin ViewSwitcher Output

Different Attributes of ViewSwitcher in XML

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

Set Id of ViewSwitcher

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 ViewSwitcher using android:id attribute like below –

    <ViewSwitcher
        android:id="@+id/viewSwitcher_ID"
        />

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

Set Width of ViewSwitcher

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

    <ViewSwitcher
        android:id="@+id/viewSwitcher_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 ViewSwitcher Dynamically

Set Height of ViewSwitcher

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

    <ViewSwitcher
        android:id="@+id/viewSwitcher_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 ViewSwitcher Dynamically

Set Padding of ViewSwitcher

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

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

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

Set Margin of ViewSwitcher

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

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

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

Set Background of ViewSwitcher

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

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

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

Set Visibility of ViewSwitcher

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

    <ViewSwitcher
        android:id="@+id/viewSwitcher_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

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

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

Different Attributes of Android ViewSwitcher Widget

Below are the various attributes that are used to customise android ViewSwitcher Widget. However, you can check the complete list of attributes of ViewSwitcher 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 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

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

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

Leave a Reply