Android ViewSwitcher - Androhub

ViewSwitcher

Android ViewSwitcher

ViewSwitcher is a sub-class of ViewAnimator, switches between two views, and has a factory from which these views are created. You can either use the factory to create the views, or add them yourself.

A 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 ViewSwitch, java.lang.IllegalStateException of “Can’t add more than 2 views to a ViewSwitcher” will happen.

Example

In this example i will demonstrate you how to work on ViewSwitcher.

VIDEO DEMO

Let’s get start by creating a project in Eclipse IDE.

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java)

2.  Create a layout file for MainActivtiy.java under res ⇒ layout folder. I named the layout file as activity_main.xml. In this layout i had taken ViewSwitcher and inside it i had taken two views one ImageView and other one is Button.

3. Now, add the following code to MainActivity.java , in this activity i am going to show how to show both views in single layout.

4. Finally, run the app and you will get the output as shown in video.

Thanks. :)

 

1 Comment

Paolo
Monday, October 31st, 2016

Thank you for the tutorial, if i wont add the fragments non in main activity but in another, (help activity), where i must put the code ? all the tutorial the i have seen is in the main activity, i try to write the code in another activity but not work i have erro, can you help me ? thank you very much, Paolo

Post comment

Your email address will not be published. Required fields are marked *