简体   繁体   中英

Set Wallpaper on Button Click in RecyclerView

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.

在此处输入图片说明

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.

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!

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);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM