To make it more visible we will obtain the path in string
format within an array.
String[] strRutas = {"R.drawable.image1","R.drawable.image2","R.drawable.image3","R.drawable.image4",
"R.drawable.image5","R.drawable.image6","R.drawable.eliminar","R.drawable.image8"};
In my case I will get this route or string
in question "R.drawable.eliminar"
.
String string = strRutas[6]; //"R.drawable.eliminar";
Now I'm going to chop this String to get just the word eliminar
String[] trozosStr = string.split("\.");
String trozo3 = trozosStr[2];
And already with this portion of code we complete the rest. Say also that you have to use setImageDrawable()
instead of setImageResource()
String uri = "@drawable/"+ trozo3;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Drawable imagenDra = ContextCompat.getDrawable(getApplicationContext(), imageResource);
imagen.setImageDrawable(imagenDra);
Result:
P.d. Finally say that it depends on the ability of each one to implement it in a method and to make it easier.