{"id":1885,"date":"2016-05-18T06:40:36","date_gmt":"2016-05-18T06:40:36","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1885"},"modified":"2019-06-12T11:52:01","modified_gmt":"2019-06-12T11:52:01","slug":"viewanimator","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/viewanimator","title":{"rendered":"ViewAnimator Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen. ViewAnimator switches smoothly between two or more views and thus provides a way of transitioning from one view to another through appropriate animations.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1875\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ViewAnimator-in-Android.gif\" alt=\"ViewAnimator in Android\" width=\"306\" height=\"458\" \/><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> \u00a0ViewAnimator is the parent class for ViewFlipper, ViewSwitcher and other views so it perform same operations. It is used for switching between different views.<\/p>\n<hr \/>\n<h4><strong>Basic\u00a0ViewAnimator XML Code:<\/strong><\/h4>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here --&gt;\r\n\r\n&lt;\/ViewAnimator&gt;<\/pre>\n<hr \/>\n<h4><strong>Steps for Implementation of ViewAnimator:<\/strong><\/h4>\n<ol>\n<li>Get the reference of ViewAnimator in class using findViewById() method, or you can also create an object dynamically.<\/li>\n<li>Add child views in ViewAnimator using switcherid.addView() method.<\/li>\n<li>Set an in-animation using switcherid.setInAnimation().<\/li>\n<li>Set an out-animation using switcherid.setOutAnimation().<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>Important Methods Of ViewAnimator:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of ViewAnimator that may be called in order to manage the ViewAnimator.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. showNext():<\/strong><\/span> This method is used to show the next view of ViewAnimator. As we discussed earlier ViewAnimator can have two or more child views of which only one is shown at a time, so this method is used to show the next views.<\/p>\n<p>Below we perform click event on button and call showNext() method to show the next view in ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/get the reference of ViewAnimator\r\n\r\nButton btnNext=(Button) findViewById(R.id.buttonNext); \/\/get the reference of Button\r\n\/\/ set Click event on next button\r\nbtnNext.setOnClickListener(new View.OnClickListener() {\r\n\r\npublic void onClick(View v) {\r\n\/\/ TODO Auto-generated method stub\r\n\/\/ show the next view of ViewAnimator\r\nsimpleViewAnimator.showNext();\r\n}\r\n});\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1877\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/shownext-in-ViewAnimator-Android.gif\" alt=\"shownext in ViewAnimator Android\" width=\"304\" height=\"455\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>2. showPrevious():<\/strong><\/span> This method is used to show the Previous view of ViewAnimator. As we discussed earlier ViewAnimator can have two or more child views of which only one is shown at a time so this method is used to show the Previous views.<\/p>\n<p>Below we perform click event on button and call showPrevious() method to show the previous view in ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\n\r\nButton btnPrevious=(Button) findViewById(R.id.buttonPrevious); \/\/ get the reference of Button\r\n\/\/ set Click event on next button\r\nbtnPrevious.setOnClickListener(new View.OnClickListener() {\r\n\r\npublic void onClick(View v) {\r\n\/\/ TODO Auto-generated method stub\r\n\/\/ show the next view of ViewFlipper\r\nsimpleViewAnimator.showPrevious();\r\n}\r\n});\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1878\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/showPrevious-in-ViewAnimator-Android.gif\" alt=\"showPrevious in ViewAnimator Android\" width=\"300\" height=\"458\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3. loadAnimation(Context context, int id):<\/strong><\/span> This method is used whenever we need to define an object of Animation class through AnimationUtilities class by calling a static method loadAnimation.<\/p>\n<p>Below we create an object of Animation class and load an animation by using AnimationUtils class.<\/p>\n<pre>\/\/ Load Animation using AnimationUtils class\r\n Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);\r\n Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. setInAnimation(in):<\/strong><\/span> This method is used to set the animation of the appearance of the object on the screen.<\/p>\n<p>Below we create an object of Animation class and load an animation by using AnimationUtils class and then set the Animation on ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id.simpleViewAnimator); \/\/ initiate a ViewAnimator\r\nAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); \/\/ load an animation\r\nsimpleViewAnimator.setInAnimation(in); \/\/ set in Animation for ViewAnimator\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1879\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setInAnimation-in-ViewAnimator-Android.gif\" alt=\"setInAnimation in ViewAnimator Android\" width=\"305\" height=\"417\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>5. setOutAnimation(out):<\/strong><\/span> This method does the opposite of setInAnimation(). Whenever we use show the next view, it first removes the old view using an animation set with the setOutAnimation() method, and then places the new one using the animation set by the setInAnimation() method.<\/p>\n<p>Below we create an object of Animation class and load an animation by using AnimationUtils class and then set the out Animation on ViewAnimator<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator); \/\/ get reference of ViewAnimator\r\nAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); \/\/ load an animation\r\nsimpleViewAnimator.setOutAnimation(out); \/\/ set out Animation for ViewAnimator\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1880\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setOutAnimation-in-ViewAnimator-Android.gif\" alt=\"setOutAnimation in ViewAnimator Android\" width=\"303\" height=\"415\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>6. addView(View child):<\/strong><\/span> This method is used to add the child view at run time in the ViewAnimator.<\/p>\n<p>Below we create a new view i.e TextView and then add that view in our ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id.simpleViewAnimator); \/\/ get reference of ViewAnimator\r\n\r\nTextView textView = new TextView(this); \/\/ create a TextView\r\ntextView.setText(\"View Animator TextView\"); \/\/ set text in TextView\r\nsimpleViewAnimator.addView(textView); \/\/ add the TextView in ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. getCurrentView():<\/strong><\/span> This method is used for getting the current displayed child view of ViewAnimator.<\/p>\n<p>Below we get the current displayed child view of ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nView view = simpleViewAnimator.getCurrentView(); \/\/ get current displayed child view of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. getDisplayedChild():<\/strong><\/span> This method is used for getting the index for current displayed child view of ViewAnimator. This method returns an int type value for current displayed child view\u2019s index.<\/p>\n<p>Below we get the index of current displayed child view of ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nint\u00a0 displayedChildIndex = simpleViewAnimator.getDisplayedChild(); \/\/ get index for current displayed child view of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>9. getInAnimation():<\/strong><\/span> This method is used to get the current animation used to animate a View that enters the screen. This method returns the in animation that we set using setInAnimation() method.<\/p>\n<p>Below we firstly set the in animation and then get the current animation that is used to animate a View that enters the screen.<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id.simpleViewAnimator); \/\/ initiate a ViewAnimator\r\nAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); \/\/ load an animation\r\nsimpleViewAnimator.setInAnimation(in); \/\/ set in Animation for ViewAnimator\r\n<\/pre>\n<p>Animation currentInAnimation=simpleViewAnimator.getInAnimation(); \/\/ get current animation that is used to animate a View that enters the screen.<\/p>\n<p><span style=\"color: #008000;\"><strong>10. getOutAnimation():<\/strong><\/span> This method is used to get the current animation used to animate a View that exits the screen.. This method returns the out animation that we set using setoutAnimation() method.<\/p>\n<p>Below we firstly set the out animation and then get the current animation that is used to animate a View that exits the screen.<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator); \/\/ get reference of ViewAnimator\r\nAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); \/\/ load an animation\r\nsimpleViewAnimator.setOutAnimation(out); \/\/ set out Animation for ViewAnimator\r\n\r\nAnimation currentOutAnimation=simpleViewAnimator.getOutAnimation(); \/\/ get current animation that is used to animate a View that exits the screen.\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>11. removeAllViews():<\/strong><\/span> This method is used to remove all child views from the ViewGroup.<\/p>\n<p>Below we remove the all child views from the ViewGroup.<\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator); \/\/ get reference of ViewAnimator\r\n\r\nsimpleViewAnimator.removeAllViews(); \/\/ remove all the child views of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>12. removeView(View\u00a0view):<\/strong> <\/span>This method is used to remove the child view of ViewAnimator. In this method we pass the object of that child view which we want to remove.<\/p>\n<p>Below we firstly get the current displayed child view of ViewAnimator and then remove that view from the ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nView view=simpleViewAnimator.getCurrentView(); \/\/ get current displayed child view of ViewAnimator\r\nsimpleViewAnimator.removeView(view); \/\/ remove the current displayed child view of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>13. removeViewAt(int index):<\/strong><\/span> This method is used to remove the view at the specified position in the group. In this method we pass the index of that child view which we want to remove.<\/p>\n<p>Below we firstly get the index of current displayed child of ViewAnimator and then remove that child view from the ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nint\u00a0 displayedChildIndex = simpleViewAnimator.getDisplayedChild(); \/\/ get index for current displayed child view of ViewAnimator\r\nsimpleViewAnimator.removeViewAt(displayedChildIndex); \/\/ remove the current displayed child view of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>14. setDisplayedChild(int whichChild):<\/strong><\/span> This method is used to set which child view will be displayed in ViewAnimator. In this method we set the index of child view that will be displayed in ViewAnimator.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nsimpleViewAnimator.setDisplayedChild(1); \/\/ set the indez of current displayed child view of ViewAnimator\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>15. setAnimateFirstView(boolean animate):<\/strong> <\/span>This method is used to indicate whether the current view should be animated the first time the ViewAnimator is displayed. In this method we set the true or false value.<\/p>\n<p>Below we set the true value for setAnimateFirstView() method.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nsimpleViewAnimator.setAnimateFirstView(true); \/\/ set true value for setAnimateFirstView\r\n<\/pre>\n<p>Using the above code first view will be animated in ViewAnimator. If you change it to false first will not be animated for the first time.<\/p>\n<p><span style=\"color: #008000;\"><strong>16. getAnimateFirstView():<\/strong><\/span> This method checks whether the current view should be animated the first time the ViewAnimator is displayed. This method returns Boolean type value means true or false.<\/p>\n<p>Below we firstly set the true value for setAnimateFirstView and then checks whether the current view should be animated the first time the ViewAnimator is displayed or not.<\/p>\n<pre>ViewAnimator simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\nsimpleViewAnimator.setAnimateFirstView(true); \/\/ set true value for setAnimateFirstView\r\nBoolean isAnimateFirstTime=simpleViewAnimator.getAnimateFirstView(); \/\/ checks whether the view should animate first time or not.\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of ViewAnimator:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a ViewAnimator that helps us to configure it in our layout (xml).<\/p>\n<p><span style=\"color: #008000;\"><strong>1. Id:<\/strong><\/span> id attribute is used to uniquely identify a ViewAnimator.<\/p>\n<pre>&lt; ViewAnimator\r\n\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\" &gt;  &lt;!-- id of the ViewAnimator used to uniquely identify it --&gt;\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ ViewAnimator &gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. animateFirstView:<\/strong><\/span> This attribute is used to set the current view should be animated the first time the ViewAnimator is displayed or not. We set Boolean value for this attribute. We can also set value programmatically means in java class using setAnimateFirstView(boolean animate) method.<\/p>\n<p>Below we set the true value for animateFirstView attribute.<\/p>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:animateFirstView=\"true\"&gt; &lt;!--set true value for animateFirstView attribute --&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ ViewAnimator &gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. inAnimation:<\/strong><\/span> This attribute is used as an Identifier for the animation to use when a view is shown.<\/p>\n<p>Below we set the slide in left animation for the inAnimation attribute.<\/p>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:animateFirstView=\"true\"\r\nandroid:inAnimation=\"@android:anim\/slide_in_left\"&gt; &lt;!-- slide in left animation for the child views of ViewAnimator--&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ ViewAnimator &gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. outAnimation:<\/strong> <\/span>This method is used as an Identifier for the animation to use when a view is hidden.<\/p>\n<p>Below we set the slide out right animation for the outAnimation attribute.<\/p>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:animateFirstView=\"true\"\r\nandroid:outAnimation=\"@android:anim\/slide_out_right\"&gt; &lt;!-- slide out right animation for the child views of ViewAnimator--&gt;\r\n\r\n&lt;!--Add View\u2019s Here--&gt;\r\n\r\n&lt;\/ViewAnimator&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>5. padding:<\/strong><\/span> This attribute is used to set the padding from left, right, top or bottom side of a ViewAnimator.<\/p>\n<ul>\n<li><strong>paddingRight:<\/strong> This attribute is used to set the padding from the right side of a ViewAnimator.<\/li>\n<li><strong>paddingLeft:<\/strong> This attribute is used to set the padding from the left side of a ViewAnimator.<\/li>\n<li><strong>paddingTop:<\/strong> This attribute is used to set the padding from the top side of a ViewAnimator.<\/li>\n<li><strong>paddingBottom:<\/strong> This attribute is used to set the padding from the bottom side of a ViewAnimator.<\/li>\n<li><strong>Padding:<\/strong> This attribute is used to set the padding from the all the side\u2019s of a ViewAnimator.<\/li>\n<\/ul>\n<p>Below we set the 10dp padding from all the sides of a ViewAnimator<\/p>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:padding=\"10dp\"&gt; &lt;!-- set 10 dp padding from all the sides of ViewAnimator --&gt;\r\n\r\n&lt;!--\u00a0Add View\u2019s Here--&gt;\r\n\r\n&lt;\/ViewAnimator&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. background:<\/strong><\/span> This attribute is used to set the background of a ViewFlipper. We can set a color or a drawable in the background of a ViewAnimator.<\/p>\n<p>Below is the example code with explanation included in which we set the red color for the background of a ViewAnimator.<\/p>\n<pre>&lt;ViewAnimator\r\nandroid:id=\"@+id\/simpleViewAnimator\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#f00\"&gt; &lt;!-- set red color in the background of ViewFlipper --&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ViewAnimator &gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1883\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/background-in-ViewAnimator-Android.jpg\" alt=\"background in ViewAnimator Android\" width=\"246\" height=\"285\" \/><\/p>\n<p><strong>Setting background In ViewAnimator in Java class:<\/strong><\/p>\n<pre>ViewAnimator simpleViewAnimator=(ViewAnimator)findViewById(R.id. simpleViewAnimator); \/\/ get reference of ViewAnimator\r\nsimpleViewAnimator.setBackgroundColor(Color.RED);\/\/ set red color in the background of ViewAnimator<\/pre>\n<hr \/>\n<h4>ViewAnimator Examples In Android Studio:<\/h4>\n<p>Below is the example of ViewAnimator in which we display a ViewAnimator with five ImageView\u2019s. Firstly we create array of images and add the ImageView\u2019s in ViewAnimator by using addView() method. We also use one Button for switching the view of ViewAnimator.<\/p>\n<p>Firstly we load and set slide in left and slide out right animation on ViewAnimator. Then finally whenever a user click on next button, ViewAnimator switch between the views and the current view will go out and next view will come in with specified animation. We also set the false\u00a0value for setAnimateFirstView to disable the animation for first view at first time.<\/p>\n<p>Below you can download complete project code, see final output and step by step explanation of the ViewAnimator example:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/github.com\/abhisheksaini4\/ViewAnimatorExample\" target=\"_blank\" rel=\"nofollow\">Download Code<\/a><a class=\"help\" title=\"Learn How To Download Code And Import In Android Studio\" href=\"\/androidstudio\/download-code-abhiandroid\" target=\"_blank\" rel=\"nofollow\"> ? <\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1887\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ViewAnimator-Example-in-Android-Studio.gif\" alt=\"ViewAnimator Example in Android Studio\" width=\"306\" height=\"458\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1:\u00a0<\/strong><\/span>Create a new project and name it ViewAnimatorExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:\u00a0<\/strong><\/span>Open res -&gt; layout -&gt;activity_main.xml (or) main.xml and add following code :<\/p>\n<p>In this step we open an xml file and add the code for displaying two Buttons and ViewAnimator by using its different attributes.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:background=\"#fff\"\r\n    android:orientation=\"vertical\"&gt;\r\n\r\n    &lt;ViewAnimator\r\n        android:id=\"@+id\/simpleViewAnimator\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"&gt;\r\n\r\n    &lt;\/ViewAnimator&gt;\r\n\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/buttonNext\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_gravity=\"center\"\r\n        android:layout_marginTop=\"150dp\"\r\n        android:background=\"#055\"\r\n        android:text=\"NEXT\"\r\n        android:textColor=\"#fff\"\r\n        android:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span>\u00a0Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the ViewAnimator and Button. In this, firstly we create array of images and add the 5 ImageView\u2019s in ViewAnimator by using addView() method. We also use one Button for switching the view of ViewAnimator.<\/p>\n<p>Here\u00a0we load and set slide in left and slide out right animation and finally whenever a user click on next button, ViewAnimator switch between the views. We also set the false value for setAnimateFirstView to disable the animation for first view at first time.<\/p>\n<pre>package com.example.ip_d.viewanimatorexample;\r\n\r\nimport android.os.Bundle;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.view.View;\r\nimport android.view.animation.Animation;\r\nimport android.view.animation.AnimationUtils;\r\nimport android.widget.Button;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\nimport android.widget.ViewAnimator;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    private ViewAnimator simpleViewAnimator;\r\n    Button btnNext;\r\n    \/\/ array of images\r\n    int[] images = {R.drawable.lion, R.drawable.cat, R.drawable.dog, R.drawable.bird1, R.drawable.bird2};\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n\r\n        setContentView(R.layout.activity_main);\r\n\r\n        \/\/ get The references of Button and ViewAnimator\r\n        btnNext = (Button) findViewById(R.id.buttonNext);\r\n        simpleViewAnimator = (ViewAnimator) findViewById(R.id.simpleViewAnimator); \/\/ get the reference of ViewAnimator\r\n        for (int i = 0; i &lt; images.length; i++) {\r\n            ImageView imageView = new ImageView(getApplicationContext()); \/\/ create a new object  for ImageView\r\n            imageView.setImageResource(images[i]); \/\/ set image resource for ImageView\r\n            simpleViewAnimator.addView(imageView); \/\/ add child view in ViewAnimator\r\n        }\r\n        \/\/ Declare in and out animations and load them using AnimationUtils class\r\n        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);\r\n        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);\r\n\r\n        \/\/ set the animation type to ViewAnimator\r\n        simpleViewAnimator.setInAnimation(in);\r\n        simpleViewAnimator.setOutAnimation(out);\r\n\r\n        simpleViewAnimator.setAnimateFirstView(false); \/\/ set false value for setAnimateFirstView\r\n\r\n        \/\/ ClickListener for NEXT button\r\n        \/\/ When clicked on Button ViewSwitcher will switch between views\r\n        \/\/ The current view will go out and next view will come in with specified animation\r\n        btnNext.setOnClickListener(new View.OnClickListener() {\r\n\r\n            public void onClick(View v) {\r\n                \/\/ TODO Auto-generated method stub\r\n                \/\/ show the next view of ViewAnimator `     `\r\n                simpleViewAnimator.showNext();\r\n            }\r\n        });\r\n\r\n    }\r\n\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and you will see\u00a0lion image on screen. Now click on NEXT button to switch to the next view in ViewAnimator.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views ( like TextView, ImageView or any layout). It is mainly useful to animate the views on screen. ViewAnimator switches smoothly between two &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/viewanimator\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ViewAnimator Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-1885","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ViewAnimator Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ViewAnimator in Android, its methods and attributes used with example in Android Studio. In Android, ViewAnimator is a sub class of FrameLayout container that is used for switching between views.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/viewanimator\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1885","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=1885"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1885\/revisions"}],"predecessor-version":[{"id":2807,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1885\/revisions\/2807"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}