-1

I am creating a wallpaper app. The app has a recycler view with an image and a button on it in each list item. The button click is used to set the wallpaper of the corresponding image to the home screen. I have successfully setup the recycler view but I have a problem in setting the wallpaper on button click.

This is my activity_main.xml code

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycleView"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">


</android.support.v7.widget.RecyclerView>

This is my MainActivity.java file

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    int images[] = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5,
                    R.drawable.pic6, R.drawable.pic7, R.drawable.pic8, R.drawable.pic9, R.drawable.pic10};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycleView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new ListAdapter(images));

    }
}

This is my ListAdapter class which extends RecyclerView.Adapter also this class has nested class ListViewHolder which extends RecyclerView.ViewHolder

public class ListAdapter extends
RecyclerView.Adapter<ListAdapter.ListViewHolder> {

    private int[] images;
    public ListAdapter(int[] images){
        this.images = images;
    }

    @NonNull
    @Override
    public ListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        View view = inflater.inflate(R.layout.list_item, viewGroup, false);
        return new ListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListViewHolder listViewHolder, int i) {
        int index = images[i];
        listViewHolder.imageView.setImageResource(index);
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

    public class ListViewHolder extends RecyclerView.ViewHolder{

        ImageView imageView;
        Button setWallpaper;

        public ListViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.my_images);
            setWallpaper = itemView.findViewById(R.id.setWallpaper);
        }
    }
}

This is my list_item.xml file

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/my_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/pic1"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"/>

    <Button
        android:id="@+id/setWallpaper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Set"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="20dp"
        android:layout_marginBottom="20dp"/>

</RelativeLayout>

This is the design of each list item.

enter image description here

Now I want to set click on the button to set the corresponding wallpaper to the home screen. I am having trouble where to put onClick() method and how to set wallpaper.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
M Umer
  • 353
  • 6
  • 13
  • Possible duplicate of [Programmatically set android phone's background](https://stackoverflow.com/questions/20053919/programmatically-set-android-phones-background) – ADM Jan 29 '19 at 11:15
  • 1
    `onClick` to button you put inside `ListViewHolder` . Use `getAdapterPosition()` to get the clicked item position inside `ListViewHolder`. – ADM Jan 29 '19 at 11:17
  • No my question is little different I have already checked alot of solutions before asking. – M Umer Jan 29 '19 at 11:19
  • ok let me try this – M Umer Jan 29 '19 at 11:19
  • I am now facing issue with WallpaperManager I dont know which context I have to pass in WallpaperManager.getInstance() method. – M Umer Jan 29 '19 at 11:25

2 Answers2

1

Inside the bindviewholder do something like this:

holder.setwallpaper.setOnClickListener(v -> {
        try {
       WallpaperManager wallpaperManager = WallpaperManager.getInstance(mcontext); 
       Drawable drawable = imageview.getDrawable(position);
//or if the above line of code doesn't work try fetching the image from your array list
imagelist.get(position).image
                Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
                wallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
        }


            });

I've given you the logic try it if you face any problem contact me.

To pass context do something like this: Inside your mainactivity class:

public void initializeAdapter()
    {
        absadapter localabsadapter = new absadapter(exlist,abs.this);
        recyclerView.setAdapter(localabsadapter);
    }

Inside your recyclerview:

Context mContext;
absadapter(List exList,Context context) {
        this.exList= exList;
        this.mContext = context;
}

Happy Coding!

Mr. Patel
  • 1,379
  • 8
  • 21
0

load image with Glide or Picasso inside onBindViewHolder, with Glide

Glide.with(this).load("image_url").into(imageView);

then set OnClickListener inside onBindViewHolder as example

holder.setWallpaper.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

inside onClick convert image to bitmap and set wallpaper. you can use Glide for that

Bitmap bitmapImage;
Glide.with(this)
                .asBitmap()
                .load("image_url")
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        bitmapImage = resource;
                    }
                });

then execute SetWallpaperTask

new SetWallpaperTask().execute();

SetWallpaperTask() class like this

private class SetWallpaperTask extends AsyncTask<Void, Void, Void> {

        @Override
        protected Long doInBackground(Void... voids) {
            try {
                WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
                wallpaperManager.setBitmap(bitmapImage);

            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
        }
    }
J.D
  • 58
  • 6