Fundamentals
Introduction
1st Android Application

OOPS Concepts
Class
Object
Abstraction
Encapsulation

Polymorphism
Inheritance
Message Passing
Dynamic Binding

UI Controls
TextView
Button
Toggle Button
CheckBox
Radio Button
Spinner
ProgressBar
SeekBar
RadioGroup
RatingBar
Switch
EditText
AutoComplete
TextView

MultiAutoComplete
TextView

ImageView
ImageButton
Gallery
MediaPlayer
VideoView
MediaController

TimePicker
DatePicker
CalendarView
Chronometer
Analog Clock
Digital Clock

Layouts
LinearLayout
RelativeLayout
GridLayout
TableLayout
FrameLayout
Fragment

Composite
ListView
ExpandableListView
GridView
ScrollView
SearchView
Sliding Drawer
Tab Widget
WebView

Transitions
Image Switcher
StackView
TextSwitcher
ViewAnimator
View Flipper
View Switcher

Advanced
Request Focus
View
View Stub
TextureView
Gesture Overlay View

SurfaceView
NumberPicker
Zoom Button
Zoom Controls
AbsoluteLayout

Database
SharedPreference
SQLite DB
MySQL DB

Webservices
Check HttpURLConnection
Single Webservice Call
Multi Webservice Call
ListView from Webservice
SQLite from Webservice
Data from Database

Telephone Manager
IMEI
IMSI
Phone Type
Sim Type
Network Type

Content Providers
Contact Provider
BookMark Provider
Own Provider


Message & Networking
Send SMS
SMS with Reports
Group SMS

Send E-Mail
Download Binary Data


Location Based Service
Display Google Map
Events with Google Map

Android Services
Create Service
Create Longtime Service

Asynchronous Task
Repeating Services
IntentService

Activity from a Service
Bind Activity to Service


Sample Projects
HMS
CNB
Login Screen
Camera Event
Browse Option
QR Code Events
Accelerometer Sensor
EditText from Spinner

Android View Switcher

  1. View Switcher Creation in Android
  2. View Switcher with Manual Controls in Android

Note                     Download ADT Plugin Here.
Here I am using,
OS : Linux (Ubuntu 12.04)
Eclipse : Juno (Version 4.2.0)
Android API Level : 3 to 'n' as per need
Emulator API Level : It will be displayed in output image

A. View Switcher Creation in Android


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ViewSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <RelativeLayout
	android:background="#0000FF"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"  >
    
	<ProgressBar
	    android:id="@+id/progressbar"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:layout_centerHorizontal="true" />
      
	<TextView
	    android:text="Loading..."
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:textSize="20dp"
	    android:textColor="#FFFFFF"
	    android:layout_centerHorizontal="true"
	    android:layout_below="@+id/progressbar" />

    </RelativeLayout>
 
    <RelativeLayout
	android:background="#FFFF00"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" >
    
	<TextView
            android:text="Finished!"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="20dp"
            android:textColor="#000000"
            android:layout_marginTop="10dp"
            android:layout_centerHorizontal="true" />
    
	<ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/progressbar"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="50dp"
            android:src="@drawable/smile" />
    
    </RelativeLayout>
 
</ViewSwitcher>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package balaji.viewswitcher_creation;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ViewSwitcher;
 
public class MainActivity extends Activity {
	//the ViewSwitcher
	private ViewSwitcher switcher;
	private static final int REFRESH_SCREEN = 1;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
		startScan();
	}
 
	public void startScan() {
		
		new Thread() {
			
			public void run() {		
				
				try{			
					// This is just a tmp sleep so that we can emulate something loading
					Thread.sleep(5000);
					// Use this handler so than you can update the UI from a thread
					Refresh.sendEmptyMessage(REFRESH_SCREEN);
				} catch(Exception e){
				}
			}
		}.start();
	}
 
	// Refresh handler, necessary for updating the UI in a/the thread
	Handler Refresh = new Handler(){
		public void handleMessage(Message msg) {
			
			switch(msg.what){
			
			case REFRESH_SCREEN:
				switcher.showNext();
				// To go back to the first view, use switcher.showPrevious()
				break;

			default:
				break;
			}
		}
	};
}

Step 4 : Open AndroidManifest.xml and add following code :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.viewswitcher_creation"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 5 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">ViewSwitcher_Creation</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 6 : Our output will be like this :

Note
In View Switcher we can add two childs only.


Output


Output









B. View Switcher with Manual Controls in Android


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ViewSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <RelativeLayout
	android:background="#0000FF"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"  >
          
	<TextView
	    android:text="1st Page View"
	    android:id="@+id/t1"
	    android:layout_height="wrap_content"
	    android:layout_width="wrap_content"
	    android:textSize="20dp"
	    android:layout_marginTop="50dp"
	    android:textColor="#FFFFFF"
	    android:layout_centerHorizontal="true"/>
    
    	<Button 
            android:text="Next Page"
            android:textColor="#FFFFFF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/t1"
            android:layout_marginTop="50dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/Next"/>
    
    </RelativeLayout>
 
    <RelativeLayout
	android:background="#FFFF00"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent" >
    
	<TextView
            android:text="2nd Page View"
            android:id="@+id/t2"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textSize="20dp"
            android:layout_marginTop="50dp"
            android:textColor="#000000"
            android:layout_centerHorizontal="true"/>
    
	<Button 
            android:text="Previous Page"
            android:textColor="#000000"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/t2"
            android:layout_marginTop="50dp"
            android:layout_centerHorizontal="true"
            android:id="@+id/Previous"/>
    
    </RelativeLayout>
 
</ViewSwitcher>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package balaji.viewswitcher_task1;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ViewSwitcher;
 
public class MainActivity extends Activity {
 
	ViewSwitcher switcher;
	Button Next, Previous;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
        
        Next = (Button) findViewById(R.id.Next);
        Previous = (Button) findViewById(R.id.Previous);
        
        Next.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			new AnimationUtils();
			switcher.setAnimation(AnimationUtils.makeInAnimation
				(getBaseContext(), true));
			switcher.showNext();
		}
	});
        
        Previous.setOnClickListener(new View.OnClickListener() {
			
		public void onClick(View v) {
			// TODO Auto-generated method stub
				
			new AnimationUtils();
			switcher.setAnimation(AnimationUtils.makeInAnimation
				(getBaseContext(), true));
			switcher.showPrevious();
		}
	});
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.viewswitcher_task1"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

Step 5 : Open res ->values ->strings.xml and add following code :

<resources>
 
    <string name="app_name">ViewSwitcher_Task1</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
 
</resources>

Step 6 : Our output will be like this :


Output


Output









SHARE THIS PAGE



product 2

product 3

product 4

Feedbacks : balaji.scz@gmail.com