{"id":1663,"date":"2016-05-04T04:29:34","date_gmt":"2016-05-04T04:29:34","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=1663"},"modified":"2019-06-12T09:41:18","modified_gmt":"2019-06-12T09:41:18","slug":"imageswitcher","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/imageswitcher","title":{"rendered":"ImageSwitcher Tutorial With Example In Android Studio"},"content":{"rendered":"<p>In Android, ImageSwitcher is a specialized ViewSwitcher that contain ImageView type children. ImageSwitcher is available in Android from v1.6+.<\/p>\n<p>It is an element of transition widget which helps us to add transitions on the images. It is mainly\u00a0 useful to animate an image on screen. ImageSwitcher switches smoothly between two images and thus provides a way of transitioning from one Image to another through appropriate animations.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1703\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ImageSwitcher-in-Android.jpg\" alt=\"ImageSwitcher in Android\" width=\"643\" height=\"317\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/ImageSwitcher-in-Android.jpg 643w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/ImageSwitcher-in-Android-300x148.jpg 300w\" sizes=\"auto, (max-width: 643px) 100vw, 643px\" \/><\/p>\n<hr \/>\n<h4><strong>Basic\u00a0ImageSwitcher code in XML:<\/strong><\/h4>\n<pre>&lt;ImageSwitcher\r\n    android:id=\"@+id\/simpleImageSwitcher\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"wrap_content\"\/&gt;<\/pre>\n<hr \/>\n<h4><strong>Steps for Implementation of ImageSwitcher:<\/strong><\/h4>\n<ol>\n<li>Get the reference of ImageSwitcher in class using findViewById() method, or you can also create an object dynamically.<\/li>\n<li>Set a factory using switcherid.setFactory()<\/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 ImageSwitcher:<\/strong><\/h4>\n<p>Let\u2019s we discuss some important methods of ImageSwitcher that may be called in order to manage the ImageSwitcher.<\/p>\n<p><span style=\"color: #008000;\"><strong>1. setFactory(ViewFactory factory):<\/strong><\/span> This method is used to create a new view for ImageSwitcher. By using this method we create a new ImageView and replace the old view with that.<\/p>\n<p>Below code\u00a0create a new view using setFactory method.<\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ get reference of ImageSwitcher\r\n\r\n\/\/ Set the ViewFactory of the ImageSwitcher that will create ImageView object when asked\r\nimageSwitcher.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 });<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. setImageDrawable(Drawable drawable): <\/strong><\/span>This method is used to set an image in\u00a0ImageSwitcher. In this we pass an image for Drawable.<\/p>\n<p>Below we set image in ImageSwitcher by using setImageDrawable method.<\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ initiate a ImageSwitcher\r\nsimpleImageSwitcher.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher)); \/\/ set drawable image in ImageSwitcher\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1732\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setImageDrawable-in-ImageSwitcher.jpg\" alt=\"setImageDrawable in ImageSwitcher\" width=\"347\" height=\"378\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/setImageDrawable-in-ImageSwitcher.jpg 347w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/setImageDrawable-in-ImageSwitcher-275x300.jpg 275w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>3. setImageResource(int resid): <\/strong><\/span>This method is also used to set an image in\u00a0image switcher. The image is passed in the form of integer id.<\/p>\n<p>Below we set image in ImageSwitcher by using setImageDrawable method.<\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ initiate a ImageSwitcher\r\nsimpleImageSwitcher.setImageResource(R.drawable.ic_launcher); \/\/ set image resource in ImageSwitcher\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. setImageURI(Uri uri): <\/strong><\/span>This method is also used to set an image in\u00a0image switcher. The image is passed in the form of URI.<\/p>\n<p>Below we set Image in ImageSwitcher from raw folder using Uri:<\/p>\n<p><em>Before using below code first create a raw name folder in res directory and save an image name image1 in that folder.<\/em><\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ get reference of ImageSwitcher\r\nsimpleImageSwitcher.setImageURI(Uri.parse(\"android.resource:\/\/\" + getPackageName() + \"\/\" + R.raw.image1)); \/\/ set Image in ImageSwitcher from raw folder using Uri\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 AnimationUtils 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 an animation by using AnimationUtils class\r\nAnimation 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<\/pre>\n<p><span style=\"color: #008000;\"><strong>6. setInAnimation(Animation inAnimation):<\/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 ImageSwitcher.<\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ initiate a ImageSwitcher\r\nAnimation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); \/\/ load an animation\r\nsimpleImageSwitcher.setInAnimation(in); \/\/ set in Animation for ImageSwitcher\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1733\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setInAnimation-in-ImageSwitcher-Android.gif\" alt=\"setInAnimation in ImageSwitcher Android\" width=\"305\" height=\"469\" \/><\/p>\n<p><span style=\"color: #008000;\"><strong>7. setOutAnimation(Animation outAnimation):<\/strong><\/span> This method does the opposite of setInAnimation().<\/p>\n<p>Whenever we use set a new Image in ImageView, 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 ImageSwitcher<\/p>\n<pre>ImageSwitcher simpleImageSwitcher=(ImageSwitcher)findViewById(R.id. simpleImageSwitcher); \/\/ get reference of ImageSwitcher\r\nAnimation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right); \/\/ load an animation\r\nsimpleImageSwitcher.setOutAnimation(out); \/\/ set out Animation for ImageSwitcher\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1734\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/setOutAnimation-in-ImageSwitcher-Android.gif\" alt=\"setOutAnimation in ImageSwitcher Android\" width=\"305\" height=\"469\" \/><\/p>\n<hr \/>\n<h4><strong>Attributes of ImageSwitcher:<\/strong><\/h4>\n<p>Now let\u2019s we discuss some common attributes of a ImageSwitcher 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 ImageSwitcher.<\/p>\n<p>Below we set the id of the ImageSwitcher that is used to uniquely identify it.<\/p>\n<pre>&lt;ImageSwitcher\r\nandroid:id=\"@+id\/simpleImageSwitcher\"\r\nandroid:layout_width=\"wrap_content\"\r\nandroid:layout_height=\"wrap_content\" \/&gt; &lt;!--\u00a0 id of ImageSwitcher that is used to uniquely identify it --&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 ImageSwitcher.<\/p>\n<ul>\n<li><strong>paddingRight: <\/strong>This attribute is used to set the padding from the right side of a ImageSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingLeft: <\/strong>This attribute is used to set the padding from the left side of a ImageSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingTop: <\/strong>This attribute is used to set the padding from the top side of a ImageSwitcher<strong>.<\/strong><\/li>\n<li><strong>paddingBottom: <\/strong>This attribute is used to set the padding from the bottom side of a ImageSwitcher<strong>.<\/strong><\/li>\n<li><strong>Padding: <\/strong>This attribute is used to set the padding from the all the side\u2019s of a ImageSwitcher<strong>.<\/strong><\/li>\n<\/ul>\n<p>Below we set the 10dp padding from all the sides of a ImageSwitcher<\/p>\n<pre>&lt;ImageSwitcher\r\nandroid:id=\"@+id\/imageSwitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"200dp\"\r\nandroid:layout_gravity=\"center_horizontal\"\r\nandroid:padding=\"10dp\" \/&gt; &lt;!-- set 10 dp padding from all the sides of TextSwitcher --&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. background:<\/strong><\/span> This attribute is used to set the background of a ImageSwitcher. We can set a color or a drawable in the background of a ImageSwitcher.<\/p>\n<p>Below is the example code with explanation included in which we set the black color for the background of a ImageSwitcher.<\/p>\n<pre>&lt;ImageSwitcher\r\nandroid:id=\"@+id\/imageSwitcher\"\r\nandroid:layout_width=\"match_parent\"\r\nandroid:layout_height=\"200dp\"\r\nandroid:layout_gravity=\"center_horizontal\"\r\nandroid:background=\"#000\" \/&gt; &lt;!-- set black color in the background of TextSwitcher --&gt;<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1736\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/padding-and-background-attribute-in-ImageSwitcher.jpg\" alt=\"padding and background attribute in ImageSwitcher\" width=\"347\" height=\"350\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/padding-and-background-attribute-in-ImageSwitcher.jpg 347w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/padding-and-background-attribute-in-ImageSwitcher-150x150.jpg 150w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2016\/05\/padding-and-background-attribute-in-ImageSwitcher-297x300.jpg 297w\" sizes=\"auto, (max-width: 347px) 100vw, 347px\" \/><\/p>\n<p><strong>Setting background In ImageSwitcher In Java class:<\/strong><\/p>\n<pre>ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); \/\/ get the reference of ImageSwitcher.\r\nimageSwitcher.setBackgroundColor(Color.BLACK); \/\/ set black color in the background of ImageSwitcher.\r\n<\/pre>\n<hr \/>\n<h4><strong>ImageSwitcher Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of ImageSwitcher in Android Studio where we display a ImageSwitcher by using its different attributes. In this we use one Button for changing the image in ImageSwitcher. Firstly we create an <a href=\"\/java\/arrays\">array<\/a> of id\u2019s Images and create dynamic ImageView using setFactory method and then load and set slide in left and slide in right animation and finally set the Image in the ImageSwitcher on Button click using setImageResource method. Whenever a user click on next button ImageSwitcher switch between the images and the current Image will go out\u00a0and next Image will come in with specified animation.<\/p>\n<p>Below you can download complete project code, see final output and step by step explanation:<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/github.com\/abhisheksaini4\/ImageSwitcherExample\" 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-1738\" src=\"\/ui\/wp-content\/uploads\/2016\/05\/ImageSwitcher-Example-in-Android-Studio.gif\" alt=\"ImageSwitcher Example in Android Studio\" width=\"300\" height=\"438\" \/><\/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 ImageSwitcherExample<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span><strong>\u00a0<\/strong>Open <strong>res -&gt; layout -&gt;activity_main.xml (or) main.xml<\/strong> 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 ImageSwitcher \u00a0by 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:orientation=\"vertical\"&gt;\r\n\r\n\r\n    &lt;ImageSwitcher\r\n        android:id=\"@+id\/simpleImageSwitcher\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"200dp\"\r\n        android:layout_gravity=\"center_horizontal\"\r\n        android:layout_marginTop=\"50dp\" \/&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_marginTop=\"150dp\"\r\n        android:layout_gravity=\"center\"\r\n        android:background=\"#050\"\r\n        android:textColor=\"#fff\"\r\n        android:textStyle=\"bold\"\r\n        android:text=\"NEXT\" \/&gt;\r\n\r\n&lt;\/LinearLayout&gt;<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3:<\/strong><\/span><strong>\u00a0<\/strong>Open <strong>src -&gt; package -&gt; MainActivity.java<\/strong><\/p>\n<p>In this step we open\u00a0MainActivity\u00a0and\u00a0add the code for initiate the ImageSwitcher and Button. In this Firstly we create an array of id\u2019s Images and create dynamic ImageView using setFactory method and then load and set slide in left and slide in right animation and finally set the Image in the ImageSwitcher on button click using setImageResource method. Whenever a user click on next button ImageSwitcher switch between the images and the current Image will go OUT and next Image will come in with specified animation.<\/p>\n<pre>package com.example.ip_d.imageswitcherexample;\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.ImageSwitcher;\r\nimport android.widget.ImageView;\r\nimport android.widget.LinearLayout;\r\nimport android.widget.ViewSwitcher;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    private ImageSwitcher simpleImageSwitcher;\r\n    Button btnNext;\r\n\r\n\r\n    \/\/ Array of Image IDs to Show In ImageSwitcher\r\n    int imageIds[] = {R.drawable.image1, R.drawable.images2, R.drawable.image3, R.drawable.images4, R.drawable.images5};\r\n    int count = imageIds.length;\r\n    \/\/ to keep current Index of ImageID array\r\n    int currentIndex = -1;\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 ImageSwitcher\r\n        btnNext = (Button) findViewById(R.id.buttonNext);\r\n        simpleImageSwitcher = (ImageSwitcher) findViewById(R.id.simpleImageSwitcher);\r\n        \/\/ Set the ViewFactory of the ImageSwitcher that will create ImageView object when asked\r\n        simpleImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {\r\n\r\n            public View makeView() {\r\n                \/\/ TODO Auto-generated method stub\r\n\r\n                \/\/ Create a new ImageView and set it's properties\r\n                ImageView imageView = new ImageView(getApplicationContext());\r\n                \/\/ set Scale type of ImageView to Fit Center\r\n                imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);\r\n                \/\/ set the Height And Width of ImageView To FIll PARENT\r\n                imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));\r\n                return imageView;\r\n            }\r\n        });\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 ImageSwitcher\r\n        simpleImageSwitcher.setInAnimation(in);\r\n        simpleImageSwitcher.setOutAnimation(out);\r\n\r\n\r\n        \/\/ ClickListener for NEXT button\r\n        \/\/ When clicked on Button ImageSwitcher will switch between Images\r\n        \/\/ The current Image will go OUT and next Image  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                currentIndex++;\r\n                \/\/  Check If index reaches maximum then reset it\r\n                if (currentIndex == count)\r\n                    currentIndex = 0;\r\n                simpleImageSwitcher.setImageResource(imageIds[currentIndex]); \/\/ set the image in ImageSwitcher\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 Next button on screen. Click on it and image will slide in. Click on next button and new image will slide in and older image will slide out. This is ImageSwitcher in Android.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Android, ImageSwitcher is a specialized ViewSwitcher that contain ImageView type children. ImageSwitcher is available in Android from v1.6+. It is an element of transition widget which helps us to add transitions on the images. It is mainly\u00a0 useful to animate an image on screen. ImageSwitcher switches smoothly between two images and thus provides a &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/imageswitcher\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ImageSwitcher 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-1663","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>ImageSwitcher Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Tutorial on ImageSwitcher, its methods and attributes used with example in Android Studio. In Android, ImageSwitcher is a specialized ViewSwitcher that contain ImageView type children.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/imageswitcher\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1663","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=1663"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1663\/revisions"}],"predecessor-version":[{"id":2788,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/1663\/revisions\/2788"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=1663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}