I’ll keep this simple. I want to set an ImageView in an AlertDialog to an image in an array of Drawables The image that I would like to set to the ImageView can be retrieved by accessing the mImages[position].
Here is a brief explanation (complete explanation below):
In short- I need a way to pass an image from my main activity to a dialog then on Cancel dismiss the dialog and on confirm set the system wallpaper (to the image passed from the main activity) then finish(); the activity.
Here’s a complete explanation:
The user is presented with a Gallery and an ImageView above the Gallery that shows a larger preview of the image that has focus in the Gallery.
The images displayed in the Gallery are setup using:
// setup wallpaper array
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addWallpapers(resources, packageName, R.array.wallpapers);
}
// setup array defining all wallpapers & define thumbnails
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
After “Set Wallpaper” Button is pressed, an AlertDialog should open with another preview of the image that had focus in the Gallery. The AlertDialog will contain a TextView with instructions, the preview of the image we are proposing to set as the wallpaper, an “Okay” Button and a “Cancel” Button. Pressing the “Okay” Button will set the image from the ImageView preview as the system wallpaper via an InputStream.
Thanks again!
private void selectWallpaper(int position) {
Toast.makeText(getBaseContext(), "Select Wallpaper", Toast.LENGTH_SHORT)
.show();
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
Context context = this;
// CharSequence text = "Wallpaper Set!";
// int duration = Toast.LENGTH_LONG;
InputStream stream = getResources().openRawResource(
mImages.get(position));
final Dialog accept = new Dialog(context);
accept.setContentView(R.layout.confirm);
accept.setTitle("Please Confirm");
TextView instructions = (TextView) accept.findViewById(R.id.textView1);
instructions.setText("Would you like to set this as your wallpaper?");
ImageView wallpreview = (ImageView) accept
.findViewById(R.id.imageView1);
wallpreview.createFromStream(stream, "test");
// SETUP cancel (no btn) listener
Button cancelbtn = (Button) accept.findViewById(R.id.button2);
cancelbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
accept.dismiss();
}
});
// SETUP Yes Btn listener
Button okbtn = (Button) accept.findViewById(R.id.button1);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// dismiss being used as placeholder, actually setting wallpaper
// will be added
accept.dismiss();
}
});
accept.show();
}
{
;
}
You can use the layout inflator to inflate your alert dialog layout and assign it to the View of the alert dialog in your main activity. You can use findViewById() to set the onClickListeners listeners for those buttons. That will ensure that your onClickListener can see mImages.