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 Image Switcher

  1. Static Image Switcher Creation in Android
  2. Dynamic Image Switcher Creation 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. Static Image 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 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
 
    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/gallery1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp" >
    </ImageSwitcher>
 
</RelativeLayout>

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

package balaji.imageswitcher_static;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ViewSwitcher.ViewFactory;
 
public class MainActivity extends Activity implements ViewFactory {
 
	int imgs[] = 
		{ 
			R.drawable.android, 
			R.drawable.cup, 
			R.drawable.donut, 
			R.drawable.eclair,
			R.drawable.froyo,
			R.drawable.ginger,
			R.drawable.honey,
			R.drawable.ics,
			R.drawable.jellybean
		};
 
	ImageSwitcher imgSwitcher;
 
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
 
		imgSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		imgSwitcher.setFactory(this);
		imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_in));
		imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
				android.R.anim.fade_out));
 
		Gallery gallery = (Gallery) findViewById(R.id.gallery1);
		gallery.setAdapter(new ImageAdapter(this));
		
		gallery.setOnItemClickListener(new OnItemClickListener() {
 
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				imgSwitcher.setImageResource(imgs[arg2]);
			}
		});
	}
 
	public class ImageAdapter extends BaseAdapter {
 
		private Context ctx;
 
		public ImageAdapter(Context c) {
			ctx = c; 
		}
 
		public int getCount() {
 
			return imgs.length;
		}
 
		public Object getItem(int arg0) {
 
			return arg0;
		}
 
		public long getItemId(int arg0) {
 
			return arg0;
		}
 
		public View getView(int arg0, View arg1, ViewGroup arg2) {
 
			ImageView iView = new ImageView(ctx);
			iView.setImageResource(imgs[arg0]);
			iView.setScaleType(ImageView.ScaleType.FIT_XY);
			iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
			return iView;
		}
	}
 
	public View makeView() {
		ImageView iView = new ImageView(this);
		iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		iView.setLayoutParams(new ImageSwitcher.LayoutParams
			(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		iView.setBackgroundColor(0xFF000000);
		return iView;
	}
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.imageswitcher_static"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="7"
        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">ImageSwitcher_Static</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









B. Dynamic Image 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 :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />
 

</RelativeLayout>

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

package balaji.imageswitcher_dynamic;
 
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;
 
public class MainActivity extends Activity implements ViewFactory{
	
	RelativeLayout rl;
	Gallery gal;
	ImageSwitcher img;
	RelativeLayout.LayoutParams params;
	
	int imgs[] = 
		{ 
			R.drawable.android, 
			R.drawable.cup, 
			R.drawable.donut, 
			R.drawable.eclair,
			R.drawable.froyo,
			R.drawable.ginger,
			R.drawable.honey,
			R.drawable.ics,
			R.drawable.jellybean
		};
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        rl = (RelativeLayout) findViewById(R.id.rl);
        gal = (Gallery) findViewById(R.id.gallery1);
        
        img = new ImageSwitcher (MainActivity.this);
        
        gal.setAdapter(new ImageAdapter(this));
        
        img.setFactory(this);
        
        img.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
        
        img.setAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
        
        params = new RelativeLayout.LayoutParams 
        		((int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
        
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        
        img.setLayoutParams(params);
        
        rl.addView(img);
        
        gal.setOnItemClickListener(new OnItemClickListener() {
 
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			
			img.setImageResource(imgs[arg2]);
		}
	});        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public class ImageAdapter extends BaseAdapter 
    {
    	private Context ctx;
 
	public ImageAdapter(Context c) {
		ctx = c; 
	}
 
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
			return imgs.length;
	}
 
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
			return position;
	}
 
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
			return position;
	}
 
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
			
		ImageView iView = new ImageView(ctx);
		iView.setImageResource(imgs[position]);
		iView.setScaleType(ImageView.ScaleType.FIT_XY);
		iView.setLayoutParams(new Gallery.LayoutParams(200, 150));
		
		return iView;
	}
    }
 
	@Override
	public View makeView() {
		// TODO Auto-generated method stub
		
		ImageView iView = new ImageView(this);
		iView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		iView.setLayoutParams(new ImageSwitcher.LayoutParams
			(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
		iView.setBackgroundColor(0xFFffffff);
		
		return iView;
	}
}

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="balaji.imageswitcher_dynamic"
    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">ImageSwitcher_Dynamic</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


Output









SHARE THIS PAGE



product 2

product 3

product 4

Feedbacks : balaji.scz@gmail.com