Neaf Plays Neaf Plays - 1 month ago 6
Android Question

How can I set a String variable on R.drawable with glide?

I'm using the glide Library to load images and I made a method that goes like this:

void alterarInformacoes(ImageView slotImagem, String image) {
Glide.with(this)
.load(R.drawable.image)
.into(slotImagem);
}


But it doesn't work, I'd like to know if there's a way to use a string with the name of the drawable so that I can use a function to dynamically set the images.

Answer Source

You can get int value of drawable resource by name with this code:

int drawableResourceId = this.getResources().getIdentifier("nameOfDrawable", "drawable", this.getPackageName());