{"id":1687,"date":"2016-05-03T05:34:02","date_gmt":"2016-05-03T05:34:02","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1687"},"modified":"2019-06-12T12:04:12","modified_gmt":"2019-06-12T12:04:12","slug":"viewswitcher","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/viewswitcher","title":{"rendered":"ViewSwitcher Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, ViewSwitcher is a sub class of ViewAnimator that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views. It is mainly useful to animate a view on screen. ViewSwitcher switches smoothly between two views (i.e. TextView, ImageView or any layout) and thus provides a way of transitioning from one view to another through appropriate animations. You can create views for a ViewSwitcher widget either using a\u00a0factory\u00a0or by adding them on your own.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1722\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ViewSwitcher-in-Android.jpg\" alt=\"ViewSwitcher in Android\" width=\"479\" height=\"381\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/ViewSwitcher-in-Android.jpg 479w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/ViewSwitcher-in-Android-300x239.jpg 300w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong><\/span> ViewSwitcher can only have two child views of which only one is shown at a time. If you have more than two child views in ViewSwitcher,\u00a0java.lang.IllegalStateException of &#8220;Can&#8217;t add more than 2 views to a ViewSwitcher&#8221; error will come.<\/p>\n<hr \/>\n<h4><strong>Basic\u00a0ViewSwitcher XML code:<\/strong><\/h4>\n<pre>&lt;ViewSwitcher\r\n\r\nandroid:id=\"@+id\/viewswitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\" &gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add Two View\u2019s Here -- &gt;\r\n\r\n&lt;\/ViewSwitcher&gt;\r\n<\/pre>\n<hr \/>\n<h4><strong>Steps for Implementation of ViewSwitcher:<\/strong><\/h4>\n<ol>\n<li>Get the reference of ViewSwitcher in class using findViewById() method, or you can also create an object dynamically.<\/li>\n<li>Set a factory using switcherid.setFactory() method<\/li>\n<li>Set an in-animation using switcher.setInAnimation()<\/li>\n<li>Set an out-animation using switcher.setOutAnimation()<\/li>\n<\/ol>\n<hr \/>\n<h4><strong>Important Methods Of ViewSwitcher:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of ViewSwitcher that may be called in order to manage the ImageSwitcher.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. getNextView():<\/strong><\/span> This method return the next view to be displayed in ViewSwitcher. This method returns the view id of next view to be displayed.<\/p>\n<p>Below we get the id of next view to be displayed in ViewSwitcher.<\/p>\n<pre>ViewSwitcher\u00a0simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); \/\/ get the reference of ViewSwitcher\r\nView nextView=simpleViewSwitcher.getNextView(); \/\/ get next view to be displayed\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. reset():<\/strong><\/span> This method is used to reset the ViewSwitcher to hide all of the existing views and to make it think that the first time animation has not yet played. Sometime we need to reset the ViewSwitcher on click event so that it behaves like first time animation has not yet played.<\/p>\n<p>Below we reset the ViewSwitcher to hide all of the existing views.<\/p>\n<pre>ViewSwitcher simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); \/\/ get the reference of ViewSwitcher\r\n\r\nsimpleViewSwitcher.reset(); \/\/ reset the ViewSwitcher to hide all of the existing views\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. showNext():<\/strong><\/span> This method is used to show the next view of ViewSwitcher. As we discussed earlier ViewSwitcher can only have two 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 ViewSwitcher.<\/p>\n<pre>ViewSwitcher simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); \/\/ get the reference of ViewSwitcher\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 ViewSwitcher\r\nsimpleViewSwitcher.showNext();\r\n}\r\n});\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. showPrevious():<\/strong><\/span> This method is used to show the Previous view of ViewSwitcher. As we discussed earlier ViewSwitcher can only have two child views of which only one is shown at a time, so this method is used to show the Previous view.<\/p>\n<p>Below we perform click event on button and call showPrevious() method to show the previous view in ViewSwitcher.<\/p>\n<pre>ViewSwitcher simpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); \/\/ get the reference of ViewSwitcher\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 previouus view of ViewSwitcher\r\nsimpleViewSwitcher.showPrevious();\r\n}\r\n});\r\n<\/pre>\n<p><strong><span style=\"color: #008000;\">5. loadAnimation(Context context, int id):<\/span> <\/strong>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>Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); \/\/ load an animation by using AnimationUtils class\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. 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 ViewSwitcher.<\/p>\n<pre>ViewSwitcher simpleViewSwitcher=(ViewSwitcher)findViewById(R.id. simpleViewSwitcher); \/\/ initiate a ViewSwitcher\r\nAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); \/\/ load an animation\r\nsimpleViewSwitcher.setInAnimation(in); \/\/ set in Animation for ViewSwitcher\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. setOutAnimation(out):<\/strong><\/span> This method does the opposite of setInAnimation().<\/p>\n<p>whenever we 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 ViewSwitcher<\/p>\n<pre>ViewSwitcher simpleViewSwitcher=(ViewSwitcher)findViewById(R.id. simpleViewSwitcher); \/\/ get reference of ViewSwitcher\r\nAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); \/\/ load an animation\r\nsimpleViewSwitcher.setOutAnimation(out); \/\/ set out Animation for ViewSwitcher\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>8. setFactory(ViewFactory factory):<\/strong><\/span> This method is used to create a new view for ViewSwitcher. By using this method we create a new view and replace the old view with that.<\/p>\n<p>Below we show how to create a new view using setFactory method.<\/p>\n<pre>ViewSwitcher simpleViewSwitcher=(ViewSwitcher)findViewById(R.id. simpleViewSwitcher); \/\/ get reference of ViewSwitcher\r\n\r\n\/\/ Set the ViewFactory of the ViewSwitcher that will create a new view ( ImageView ) object when asked\r\nsimpleViewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {\r\n\r\npublic View makeView() {\r\n\/\/ TODO Auto-generated method stub\r\n\r\n\/\/ Create a new ImageView and set it's properties\r\nImageView imageView = new ImageView(getApplicationContext());\r\nimageView.setScaleType(ImageView.ScaleType.FIT_CENTER);\r\nimageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));\r\nreturn imageView;\r\n}\r\n});\r\n<\/pre>\n<hr \/>\n<h4><strong>Attributes of ViewSwitcher:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a ViewSwitcher that helps us to configure it in our layout (xml).<\/p>\n<p><strong><span style=\"color: #008000;\">1. id:<\/span>\u00a0<\/strong>id attribute is used to uniquely identify a ViewSwitcher.<\/p>\n<pre>&lt;ViewSwitcher\r\n\r\nandroid:id=\"@+id\/viewswitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\" &gt; &lt;!--\u00a0 id of ViewSwitcher that is used to uniquely identify it --&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ViewSwitcher&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. padding: <\/strong><\/span>This attribute is used to set the padding from left, right, top or bottom side of a ViewSwitcher.<\/p>\n<ul>\n<li><strong>paddingRight: <\/strong>This attribute is used to set the padding from the right side of a ViewSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingLeft: <\/strong>This attribute is used to set the padding from the left side of a ViewSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingTop: <\/strong>This attribute is used to set the padding from the top side of a ViewSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingBottom: <\/strong>This attribute is used to set the padding from the bottom side of a ViewSwitcher<strong>.<\/strong><\/li>\n<li><strong>Padding: <\/strong>This attribute is used to set the padding from the all the side\u2019s of a ViewSwitcher<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 10dp padding from all the sides of a ViewSwitcher<\/p>\n<pre>&lt;ViewSwitcher\r\nandroid:id=\"@+id\/simpleViewSwitcher\"\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 ViewSwitcher --&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ViewSwitcher&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1723\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/padding-in-ViewSwitcher-Android.jpg\" alt=\"padding in ViewSwitcher Android\" width=\"239\" height=\"274\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span> This attribute is used to set the background of a ViewSwitcher. We can set a color or a drawable in the background of a background: This attribute is used to set the background of a ViewSwitcher.<\/p>\n<p>Below we set the green color for the background of a background: This attribute is used to set the background of a ViewSwitcher.<\/p>\n<pre>&lt;ViewSwitcher\r\nandroid:id=\"@+id\/simpleViewSwitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:background=\"#0f0\"&gt; &lt;!-- set green color in the background of ViewSwitcher --&gt;\r\n\r\n&lt;!--\u00a0\u00a0 Add View\u2019s Here -- &gt;\r\n\r\n&lt;\/ViewSwitcher&gt;\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1724\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/background-in-ViewSwitcher-Android.jpg\" alt=\"background in ViewSwitcher Android\" width=\"233\" height=\"283\" \/><\/p>\n<p><strong>Setting Background In ViewSwitcher Java class:<\/strong><\/p>\n<pre>ViewSwitcher simpleViewSwitcher=(ViewSwitcher)findViewById(R.id. simpleViewSwitcher); \/\/ get reference of ViewSwitcher\r\nsimpleViewSwitcher.setBackgroundColor(Color.GREEN);\/\/ set green color in the background of ImageSwitcher.\r\n<\/pre>\n<hr \/>\n<h4><strong>ViewSwitcher Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of ViewSwitcher in which we display a ViewSwitcher with two views, one is ImageView and other is a layout in which we have a Button and a TextView. In this example we also use one Button for switching the view of ViewSwitcher. Firstly we load and set slide in left and slide in right animation and finally whenever a user click on next button, ViewSwitcher switch between the views and the current view will go out and next view will come in with specified animation.<\/p>\n<p>Below you can download complete Android Studio project code, see final output and step by step explanation of the example:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ViewSwitcherExample\" 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-1726\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ViewSwitcher-Example-in-Android-Studio.gif\" alt=\"ViewSwitcher Example in Android Studio\" width=\"304\" height=\"472\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>Step 1<\/strong><\/span><strong><span style=\"color: #008000;\">:<\/span>\u00a0<\/strong>Create a new project and name it ViewSwitcherExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span><strong>\u00a0<\/strong>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 a Button and a ViewSwitcher by using its different attributes.<\/p>\n<pre>&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"match_parent\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;!-- ViewSwitcher with two views one is ImageView and other is LinearLayout in which we have a Button and a TextView --&gt;\r\n&lt;ViewSwitcher\r\nandroid:id=\"@+id\/simpleViewSwitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"&gt;\r\n\r\n&lt;ImageView\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:src=\"@drawable\/image\" \/&gt;\r\n\r\n&lt;LinearLayout\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:gravity=\"center\"\r\nandroid:orientation=\"vertical\"&gt;\r\n\r\n&lt;TextView\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"LinearLayout 2\" \/&gt;\r\n\r\n&lt;Button\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:text=\"- Button 2 -\" \/&gt;\r\n\r\n\r\n&lt;\/LinearLayout&gt;\r\n&lt;\/ViewSwitcher&gt;\r\n\r\n\r\n&lt;Button\r\nandroid:id=\"@+id\/buttonNext\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\"\r\nandroid:layout_gravity=\"center\"\r\nandroid:layout_marginTop=\"150dp\"\r\nandroid:background=\"#005\"\r\nandroid:text=\"NEXT\"\r\nandroid:textColor=\"#fff\"\r\nandroid:textStyle=\"bold\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span><strong>\u00a0<\/strong>Open\u00a0 \u00a0src -&gt; package -&gt; MainActivity.java<\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code to\u00a0initiate the ViewSwitcher and Button. In this firstly, we load and set slide in left and slide in right animation and finally whenever a user click on next button, ViewSwitcher switch between the views and the current view will go out and next view will come in with specified animation.<\/p>\n<pre>package com.example.ip_d.viewswitcherexample;\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.ViewSwitcher;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\nprivate ViewSwitcher simpleViewSwitcher;\r\nButton btnNext;\r\n\r\n@Override\r\nprotected void onCreate(Bundle savedInstanceState) {\r\nsuper.onCreate(savedInstanceState);\r\n\r\nsetContentView(R.layout.activity_main);\r\n\r\n\/\/ get The references of Button and ViewSwitcher\r\nbtnNext = (Button) findViewById(R.id.buttonNext);\r\nsimpleViewSwitcher = (ViewSwitcher) findViewById(R.id.simpleViewSwitcher); \/\/ get the reference of ViewSwitcher\r\n\/\/ Declare in and out animations and load them using AnimationUtils class\r\nAnimation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);\r\nAnimation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);\r\n\r\n\/\/ set the animation type to ViewSwitcher\r\nsimpleViewSwitcher.setInAnimation(in);\r\nsimpleViewSwitcher.setOutAnimation(out);\r\n\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\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 ViewSwitcher\r\nsimpleViewSwitcher.showNext();\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 bird image in ImageView. Now click on the Next button and you will see view will switch to the 2nd view.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ViewSwitcher is a sub class of ViewAnimator that is used for switching between views. It is an element of transition widget which helps us to add transitions on the views. It is mainly useful to animate a view on screen. ViewSwitcher switches smoothly between two views (i.e. TextView, ImageView or any layout) and &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/viewswitcher\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ViewSwitcher 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-1687","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ViewSwitcher Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ViewSwitcher In Android which is a sub class of ViewAnimator used for switching between views. Also find ViewSwitcher example in Android Studio with step by step explanation.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/viewswitcher\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1687","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=1687"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1687\/revisions"}],"predecessor-version":[{"id":2813,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1687\/revisions\/2813"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}