Issue
I have many images in the drawable folder. I want to make the currently selected image as the wall paper on button click.
int[] resources = {
R.drawable.tssr_1,
R.drawable.tssr_2,
R.drawable.tssr_3,
R.drawable.tssr_4,
R.drawable.tssr_5,
R.drawable.tssr_6
};
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
//set wallpaper picture from resource here
myWallpaperManager.setResource(R.drawable.tssr_1);
//showToast("Success set as wallpaper");
} catch (IOException e) {
//showToast("Error set as wallpaper");
}
Here the R.drawable.tssr_1
is hardcoded so the application successfully sets the tssr_1
image as the wall paper image. I want to set the image that is currently showing on the screen. How do I get that image's ID?
Solution
ViewFlipper vf = new ViewFlipper(getApplicationContext());
ImageView image = new ImageView(getApplicationContext());
// set resId as image's tag, then add to viewflipper.
image.setTag(imageResId);
vf.addView(image);
// the listener will be invoked when image in flipper is clicked and held,
// then you can set the image as your wall paper.
vf.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
int resId = (Integer) v.getTag(); // get image tag
myWallpaperManager.setResource(resId);
return false;
}
});
Answered By - Weiyi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.