Android ViewSwitcher Tutorial With Example

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 viewSwitcher widget in any android application. We will also go through different attributes of viewSwitcher widget that can be used to customise it.

Output

Tutorialwing Android ViewSwitcher Output Android ViewSwitcher Tutorial With Example

Tutorialwing Android ViewSwitcher Output

Getting Started

ViewSwitcher widget can be defined as below –

ViewSwitcher is a viewAnimator that are used to switch between two views. You can either use viewFactory to create views or add them yourself. A ViewSwitcher can only have two child of which only one is shown at a time.

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 Specifies whether to animate first view when the viewAnimation is first displayed
2 android:inAnimation Specifies the animation to be used when view is shown
3 android:outAnimation Specifies 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 Specifies the gravity of the foreground drawable
2 android:measureAllChildren Specifies 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 Specifies whether LayoutTransition should run whenever there is any changes in layout
2 android:animationCache Specifies whether layout animations should create a drawing cache for their children
3 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
4 android:layoutAnimation Specifies the layout animation to use the first time the ViewGroup is laid out
5 android:layoutMode Specifies 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 Specifies the alpha of the view
2 android:background Specifies the background of the view
3 android:backgroundTint Specifies the tint to apply to background drawable
4 android:backgroundTintMode Specifies blending mode to apply to background tint
5 android:clickable Specifies whether view is clickable or not
6 android:elevation Specifies z-depth of the view
7 android:focusable Specifies whether this view can take focus or not
8 android:foreground Specifies drawable to be used to show above this view



Example of Android ViewSwitcher Widget

At first, we will create android application. Then, we will use viewSwitcher widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as ViewSwitcher. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use viewSwitcher widget in the application.

2. Modify Values folder

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>

3. Download Drawable Resources Needed

You will need images, stored in main/res/drawable folder, that are needed in the application. These drawable images are used by viewSwitcher to create different views.

4. Use ViewSwitcher Widget in xml file

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">

	<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/viewFlipper"
		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>

In activity_main.xml file, we have used ViewSwitcher widget. Note that there are only two child views of ViewSwitcher. Only one view will be shown at a time. Now, we will access these widgets in java file to perform some operations on it.

5. Access ViewSwitcher Widget in java file

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

package com.tutorialwing.viewswitcher;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ViewSwitcher viewSwitcher = findViewById(R.id.viewFlipper);
		if (viewSwitcher != null) {
			Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
			viewSwitcher.setInAnimation(in);

			Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
			viewSwitcher.setOutAnimation(out);
		}

		Button button = findViewById(R.id.btnNext);
		if (button != null) {
			button.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (viewSwitcher != null) {
						viewSwitcher.showNext();
					}
				}
			});
		}
	}
}

In MainActivity.java file, we have accessed viewSwitcher and Button widgets. We have set in and out animations that are used whenever view is switched. Button will be used to switch between different view in viewSwitcher. We have set click listener to show next view in 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

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.viewswitcher"
		  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 tutorial on Android ViewSwitcher widget.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
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