Android View Switcher
Android View Switcher
- View Switcher Creation in Android
- View Switcher with Manual Controls in Android
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_centerHorizontal="true"
android:layout_marginTop="50dp"
android:src="@drawable/android" />
</RelativeLayout>
</ViewSwitcher>
Step 3 : Open src -> package -> MainActivity.java and add following code :
package com.gudivada.hemanthsomaraju;
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 :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5 : Our output will be like this :
Note
In View Switcher we can add two childs only. |
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 com.gudivada.hemanthsomaraju;
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.main, menu);
return true;
}
}
Step 4 : Open AndroidManifest.xml and add following code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gudivada.hemanthsomaraju"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.gudivada.hemanthsomaraju.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5 : Our output will be like this :