package com.ann;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class ImageGalleryExpActivity extends Activity {
TextView myselection;
Gallery mygallery;
ImageView img;
public int[] myImageIds = { R.drawable.apple,R.drawable.baby,
R.drawable.candle, R.drawable.flower,R.drawable.cat,R.drawable.images };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myselection =(TextView)findViewById(R.id.textView1);
img=(ImageView)findViewById(R.id.imageView1);
// Bind the gallery defined in the main.xml
// Apply a new (customized) ImageAdapter to it.
mygallery = (Gallery) findViewById(R.id.gallery1);
mygallery.setAdapter(new ImageAdapter(this));
//ImageAdapter imga;
mygallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
int pos=position+1;
switch (pos) {
case 1:
{
myselection.setText(" selected image is Apple" );
}
break;
case 2:
{
myselection.setText(" selected image is Baby" );
}
break;
case 3:
{
myselection.setText(" selected image is candle" );
}
break;
case 4:
{
myselection.setText(" selected image is Flower" );
}
break;
case 5:
{
myselection.setText(" selected image is Cat" );
}
break;
case 6:
{
myselection.setText(" selected image is Rose");
}
break;
default:
break;
}
Toast.makeText(ImageGalleryExpActivity.this,"selected image is at position "+pos,Toast.LENGTH_LONG).show();
//img.setImageResource(myImageIds[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
myselection.setText("Nothing selected");
}
});
}
public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) {
this.myContext = c;
}
// inherited abstract methods - must be implemented
// Returns count of images, and individual IDs
public int getCount() {
return myImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// Returns a new ImageView to be displayed,
public View getView(int position, View convertView,
ViewGroup parent) {
// Get a View to display image data
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(myImageIds[position]);
// Set the Width & Height of the individual images
iv.setLayoutParams(new Gallery.LayoutParams(150,100));
return iv;
}}
}
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;
public class ImageGalleryExpActivity extends Activity {
TextView myselection;
Gallery mygallery;
ImageView img;
public int[] myImageIds = { R.drawable.apple,R.drawable.baby,
R.drawable.candle, R.drawable.flower,R.drawable.cat,R.drawable.images };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myselection =(TextView)findViewById(R.id.textView1);
img=(ImageView)findViewById(R.id.imageView1);
// Bind the gallery defined in the main.xml
// Apply a new (customized) ImageAdapter to it.
mygallery = (Gallery) findViewById(R.id.gallery1);
mygallery.setAdapter(new ImageAdapter(this));
//ImageAdapter imga;
mygallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
int pos=position+1;
switch (pos) {
case 1:
{
myselection.setText(" selected image is Apple" );
}
break;
case 2:
{
myselection.setText(" selected image is Baby" );
}
break;
case 3:
{
myselection.setText(" selected image is candle" );
}
break;
case 4:
{
myselection.setText(" selected image is Flower" );
}
break;
case 5:
{
myselection.setText(" selected image is Cat" );
}
break;
case 6:
{
myselection.setText(" selected image is Rose");
}
break;
default:
break;
}
Toast.makeText(ImageGalleryExpActivity.this,"selected image is at position "+pos,Toast.LENGTH_LONG).show();
//img.setImageResource(myImageIds[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
myselection.setText("Nothing selected");
}
});
}
public class ImageAdapter extends BaseAdapter {
/** The parent context */
private Context myContext;
/** Simple Constructor saving the 'parent' context. */
public ImageAdapter(Context c) {
this.myContext = c;
}
// inherited abstract methods - must be implemented
// Returns count of images, and individual IDs
public int getCount() {
return myImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// Returns a new ImageView to be displayed,
public View getView(int position, View convertView,
ViewGroup parent) {
// Get a View to display image data
ImageView iv = new ImageView(this.myContext);
iv.setImageResource(myImageIds[position]);
// Set the Width & Height of the individual images
iv.setLayoutParams(new Gallery.LayoutParams(150,100));
return iv;
}}
}
No comments:
Post a Comment