I am trying to make a wallpaper application where the user can swipe the image and when he finds a proper image he can set the image as the wallpaper .But I am not able to set the wallpaper manager here is my code
Custome swipe adapte class`package com.example.neelaysrivastava.pr12;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Button;
import java.io.IOException;
public class CustomeSwipeAdapter extends PagerAdapter {
private int [] image_resource= {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5,R.drawable.img6,
R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,
R.drawable.img14,R.drawable.img15,R.drawable.img16,
R.drawable.img17,R.drawable.img18,R.drawable.img19,R.drawable.img20};
private Context ctx;
private LayoutInflater layoutInflater;
public CustomeSwipeAdapter(Context ctx){
this.ctx=ctx;
}
@Override
public int getCount() {
return image_resource.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view==(LinearLayout)object);
}
Bitmap goal;
@Override
public Object instantiateItem(ViewGroup container, final int position) {
layoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view =layoutInflater.inflate(R.layout.second,container,false);
ImageView imageView =(ImageView)item_view.findViewById(R.id.imageView);
Button button = (Button)item_view.findViewById(R.id.button);
imageView.setImageResource(image_resource[position]);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goal= BitmapFactory.decodeResource(Resources.getSystem(),image_resource[position]);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctx.getApplicationContext());
try {
wallpaperManager.setBitmap(goal);
} catch (IOException e) {
e.printStackTrace();
}
}
});
container.addView(item_view);
return item_view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((LinearLayout)object);
}
}
`
You can try something like that:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(ctx.getApplicationContext());
try {
int height = wallpaperManager.getDesiredMinimumHeight();
int width = wallpaperManager.getDesiredMinimumWidth();
Bitmap bm = BitmapFactory.decodeResource(v.getContext().getResources(), image_resource[position]);
goal = Bitmap.createScaledBitmap(bm, width, height, true); v.getContext().getResources().getDrawable(image_resource[position]);
wallpaperManager.setBitmap(goal);
} catch (IOException e) {
e.printStackTrace();
}
}
});
Just to make sure, don't forget permission:
<uses-permission android:name="android.permission.SET_WALLPAPER" />