Android ViewFlipper Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android viewFlipper widget in any android application. We will also go through different attributes of viewFlipper widget that can be used to customise it.

Output

Tutorialwing Android ViewFlipper Output Android ViewFlipper Tutorial With Example

Tutorialwing Android ViewFlipper Output

Getting Started

ViewFlipper widget can be defined as below –

ViewFlipper is simple widget that are used to switch between views that have been added to it. In this widget, only one child is shown at a time.

Attributes of Android ViewFlipper Widget

Some of the popular attributes of android viewFlipper widget are –

Sr. XML Attributes Description
1 android:autoStart If set true, it will automatically start animating views.
2 android:flipInterval Defines the time interval after which view will be flipped

Some of the popular attributes of android ViewFlipper 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 ViewFlipper 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 ViewFlipper 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 ViewFlipper 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 ViewFlipper Widget

At first, we will create android application. Then, we will use viewFlipper 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 ViewFlipper. 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 viewFlipper 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">ViewFlipper</string>
	<string name="pause">Pause</string>
	<string name="play">Play</string>
	<string name="flipping_started">Flipping Started...</string>
	<string name="flipping_stopped">Flipping Stopped...</string>
</resources>

3. Download Drawable Resources Needed

You need some images, stored in res/drawable folder, that will be used by viewFlipper widget in the application. You may click here to download the image.

4. Use ViewFlipper 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/playPause"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_gravity="center_horizontal"
		android:layout_marginTop="15dp"
		android:text="Pause"/>

	<ViewFlipper
		android:id="@+id/viewFlipper"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:autoStart="true"
		android:flipInterval="2000">
	</ViewFlipper>

</LinearLayout>

In activtiy_main.xml file, we have defined viewFlipper widget. Attribute android:autoStart=”” is used to define whether view switching should start automatically or not. If set true, it means views will automatically be switched. Attribute android:flipInterval=”” is used to set time interval after which view will be switched.

5. Access ViewFlipper Widget in java file

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

package com.tutorialwing.viewflipper;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

	int[] imageList = {R.drawable.guava, R.drawable.jackfruit, R.drawable.mix_fruit,
			R.drawable.pomegranate, R.drawable.strawberry, R.drawable.zespri_kiwi};

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

		final ViewFlipper viewFlipper = findViewById(R.id.viewFlipper);
		if (viewFlipper != null) {
			viewFlipper.setInAnimation(getApplicationContext(), android.R.anim.slide_in_left);
			viewFlipper.setOutAnimation(getApplicationContext(), android.R.anim.slide_out_right);
		}

		if (viewFlipper != null) {
			for (int image : imageList) {
				ImageView imageView = new ImageView(this);
				FrameLayout.LayoutParams layoutParams =
						new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
				layoutParams.setMargins(30, 30, 30, 30);
				layoutParams.gravity = Gravity.CENTER;
				imageView.setLayoutParams(layoutParams);
				imageView.setImageResource(image);
				viewFlipper.addView(imageView);
			}
		}

		final Button btnPlayPause = findViewById(R.id.playPause);
		if (btnPlayPause != null && viewFlipper != null) {
			btnPlayPause.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					if (viewFlipper.isFlipping())
						viewFlipper.stopFlipping();
					else
						viewFlipper.startFlipping();

					btnPlayPause.setText(getString(viewFlipper.isFlipping() ? R.string.pause : R.string.play));
					Toast.makeText(MainActivity.this, getString(viewFlipper.isFlipping() ? R.string.flipping_started : R.string.flipping_stopped), Toast.LENGTH_SHORT).show();
				}
			});
		}
	}
}

In MainActivity.java file, we have accessed viewFlipper widget. Then, we have set in and out animations. in animation is used when view is shown. out animation is used when view is hidden. Then, we have created ImageView widget dynamically. The number of imageView is equal to length of imageList array. We have also added these imageView into viewFlipper. After that, we have accessed button, having id playPause. Then, we have set click listener into it. Whenever we click on this button child views in viewFlipper starts flipping, if it is not flipping. Otherwise, it stops flipping. We are also showing a toast message that displays the current status of viewFlipper.

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.viewflipper"
		  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 ViewFlipper widget.

Leave a Reply