I'm developing an app which needs to set some wallpapers on the phone from my drawables folder, but I have to write code for each file separately. this is my code:
fun setWallpaper(number:Int) {
val wallpaperManager = WallpaperManager.getInstance(context)
val i: Int = when(number){
0->R.drawable.wp1.toInt()
1->R.drawable.wp2.toInt()
2->R.drawable.wp3.toInt()
3->R.drawable.wp4.toInt()
4->R.drawable.wp5.toInt()
5->R.drawable.wp6.toInt()
6->R.drawable.wp7.toInt()
7->R.drawable.wp8.toInt()
8->R.drawable.wp9.toInt()
9->R.drawable.wp10.toInt()
else->0
}
wallpaperManager.setResource(i)
}
the number variable is the position I get from recyclerview. but this does not seem like a good way and is not dynamic, I can't write a line of code for each file when I have 100 files or so is there a way that I can use to kinda connect a variable to filenames? how can I do it? thanks a lot