Demo18Activity.java
package ps.demo18;
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.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class Demo18Activity extends Activity implements OnItemClickListener {
/**
* www.master-gtu.blogspot.com
* pankaj sharma(8460479175),
* chavda vijay(8460420769)
*/
Gallery gv;
int images[]={R.drawable.photo1,R.drawable.photo2,R.drawable.photo3,R.drawable.photo4};
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv=(Gallery) findViewById(R.id.gallery1);
img=(ImageView) findViewById(R.id.imageView1);
gv.setAdapter(new arrayAdapter(this));
gv.setOnItemClickListener(this);
}
private class arrayAdapter extends BaseAdapter
{
int images[]={R.drawable.photo1,R.drawable.photo2,R.drawable.photo3,R.drawable.photo4};
Context c;
public arrayAdapter(Context context)
{
c=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView img = new ImageView(c);
img.setImageResource(images[arg0]);
img.setLayoutParams(new Gallery.LayoutParams(100, 200));
return img;
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
img.setImageResource(images[arg2]);
Toast.makeText(this, images[arg2]+" selected", 1).show();
}
}
No comments:
Post a Comment