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.