ViewSwitcher in Android

ViewSwitcher in Android allows you to switch between different Layouts in same Activity.


Here in our example we will consider two Layouts and we will display only one which ever is necessary.
Let us add textView in first Layout which has the text Internet Connection is Available, when Internet Connection is Available. The other Layout has a textView which has the text Internet Connection is Unavailable.

Designing View Switcher in xml file
The Layout begins with View Switcher and two different Layouts inside it.
File: res/layout/activity_main.xml this can differ as per your program
view plaincopy to clipboardprint?
  1. <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/ViewSwitcher1"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent" >  
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="match_parent"  
  9.         android:orientation="vertical" >  
  10.     <TextView  
  11.             android:id="@+id/textView1"  
  12.             android:text="Internet Connection is Available"  
  13.             ....  
  14.             android:textStyle="bold" />  
  15.     </LinearLayout>  
  16.     <LinearLayout  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="match_parent"  
  19.         android:orientation="vertical" >  
  20.         <TextView  
  21.             android:id="@+id/textView2"  
  22.             android:text="Internet Connection is Unavailable"  
  23.             ....  
  24.             android:textStyle="bold" />  
  25.     </LinearLayout>  
  26. </ViewSwitcher>  
How to switch between Layouts.
showNext and showPrevious can help you switch between Layouts.
  1. switcher.showNext(); /* Displays second Layout */  
  2. switcher.showPrevious(); /* Displays First Layout when Second Layout is in Display*/  
How can we use it in the Program. 
In my example i have a method isOnline which returns true when the Internet connection is Available and returns false when the Internet connection is Unavailable. Using this we will show first Layout when isOnline returns true and display second Layout when isOnline returns false.

File: src/com.techirsh.viewswitcher/MainActivity.java this may also differ as per your program
view plaincopy to clipboardprint?
  1. import android.net.ConnectivityManager;  
  2. ....  
  3. import android.widget.ViewSwitcher;  
  4. public class MainActivity extends Activity {  
  5.     private ViewSwitcher switcher;    /* declaring switcher */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher1); /* ViewSwitcher1 is Switcher in xml layout */  
  11.         if(isOnline()){ /* Checks Condition, if true */  
  12.             /* Displays Default Layout */  
  13.         }else/* when the method 'isOnline' returns false Next Layout will be displayed */  
  14.             switcher.showNext();  
  15.         }  
  16.     }  
  17.     /* A Method to identify weather Internet Connection is Active or not */  
  18.     /* This will return true if Connection is Available else will return false */  
  19.     public boolean isOnline() {  
  20.         ConnectivityManager cm =  
  21.             (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  22.         NetworkInfo netInfo = cm.getActiveNetworkInfo();  
  23.         if (netInfo != null && netInfo.isConnectedOrConnecting()) {  
  24.             return true;  
  25.         }  
  26.         return false;  
  27.     } /* Method Ends */  
  28. }  
Download Source Code