I have two activities: one displays an image and a button, the other displays a photo gallery. I want to be able to select any of the images in the gallery and then display them on the first activity in place of the default image.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(PhotoGallery.this, "Position: " + id, Toast.LENGTH_SHORT).show();
Intent intent=new Intent();
intent.putExtra("PictureID", position);
setResult(RESULT_OK, intent);
finish();
}
});
}
Am I even close here? I’m not quite sure what to do with any string or int I would attach to the Intent — can I attach the object itself? I’d much rather pass back at minimum the string name of the resource (image), but the only thing I can seem to figure out right now is how to pass back the position of the picture … not a great solution. I can clarify more if necessary — thanks.
EDIT:
For anyone who comes across this in the future, here’s the result of my app. Simple, but works!
http://www.youtube.com/watch?v=WJjEvwy8yDc
As you are new to Android you should take a look at the Android samples provided with the SDk. What you want to do can be achieved by an ImageSwitcher. You can take a look at the ImageSwitcher example in the Android ApiDemos sample.
Sample from Android samples :
ImageSwitcher1.java
image_switcher_1.xml