Android Image Switcher
Android Image Switcher
- Static Image Switcher Creation in Android
- Dynamic Image Switcher Creation in Android
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 com.gudivada.hemanthsomaraju;
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.amazon,
R.drawable.apple,
R.drawable.ask,
R.drawable.dropbox,
R.drawable.email,
R.drawable.fb,
R.drawable.google,
R.drawable.linkedin
};
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 :
<?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 :
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 com.gudivada.hemanthsomaraju;
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.amazon,
R.drawable.apple,
R.drawable.ask,
R.drawable.dropbox,
R.drawable.email,
R.drawable.fb,
R.drawable.linkedin,
R.drawable.twitter
};
@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.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 :
<?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 :