Create an Android ViewAnimator Programmatically in Android

Hello Readers! In this post, we are going to learn how to create and use android viewAnimator programmatically in any android application. We will also learn to add viewAnimator in linearLayout programmatically in any application.

Output

Tutorialwing Android Dynamic ViewAnimator Output Android ViewAnimator Programmatically in Android

Tutorialwing Android Dynamic ViewAnimator Output

Getting Started

At first, we will create an android application. Then, we will use viewAnimator widget in the application.

1. Creating New Project

Follow the steps below to create a 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, 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 android viewAnimator programmatically.

2. Modify values folder

Open res/values/strings.xml file and 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 Prev and Next buttons. We have also defined a linearLayout, with id rootContainer, that will act as container for the ViewAnimator created programmatically in the application.

4. Create Android ViewAnimator Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicviewanimator/MainActivity.java file and 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.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

public class MainActivity extends AppCompatActivity {

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

		final ViewAnimator viewAnimator = new ViewAnimator(this);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		layoutParams.setMargins(30, 30, 30, 30);
		viewAnimator.setLayoutParams(layoutParams);

		Animation animationIn = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
		Animation animationOut = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);

		viewAnimator.setInAnimation(animationIn);
		viewAnimator.setOutAnimation(animationOut);

		TextView textView = new TextView(this);
		LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		textView.setLayoutParams(textParams);
		textView.setText(getString(R.string.item_post_1));
		textView.setTextColor(Color.RED);
		viewAnimator.addView(textView);

		Button button = new Button(this);
		LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		button.setLayoutParams(buttonParams);
		button.setText(getString(R.string.item_post_2));
		button.setTextColor(Color.RED);
		viewAnimator.addView(button);

		ImageView imageView = new ImageView(this);
		LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		imageView.setLayoutParams(imageParams);
		imageView.setImageResource(R.mipmap.ic_launcher);
		viewAnimator.addView(imageView);

		TextView textView2 = new TextView(this);
		LinearLayout.LayoutParams textParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		textView2.setLayoutParams(textParams2);
		textView2.setText(getString(R.string.item_post_4));
		textView2.setTextColor(Color.RED);
		viewAnimator.addView(textView2);

		LinearLayout linearLayout = findViewById(R.id.rootContainer);
		if (linearLayout != null) {
			linearLayout.addView(viewAnimator);
		}

		Button buttonPrev = findViewById(R.id.buttonPrev);
		buttonPrev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				viewAnimator.showPrevious();
			}
		});

		Button buttonNext = findViewById(R.id.buttonNext);
		buttonNext.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				viewAnimator.showNext();
			}
		});
	}
}

In MainActivity.java file, we have done below things –
a. We have created ViewAnimator programmatically. Then, we have set layout params, margins in it.
b. We have created different views dynamically to be stored in ViewAnimator. We will switch between these views using viewAnimator. There are four views(TextView, Button, ImageView and TextView in order with position) created dynamically.
c. Then, we have added this viewAnimator in linearLayout having id rootContainer.
d. Then, we have set click listener in Prev and Next buttons that will replace current view in viewAnimator with previous or next view respectively.

Since AndroidManifest.xml file is very important in any android project. We are also going to see the content inside this file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

<?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 application, we will get output as shown above.

That’s the end of tutorial on Creating Android ViewAnimator Programmatically.

Leave a Reply