2

I want to do

String childImg = "childIcon";
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.????, 0, 0, 0);

it's expecting an int but i have a string. how do i work around this?

124697
  • 22,097
  • 68
  • 188
  • 315
  • possible duplicate of [Android, getting resource ID from string?](http://stackoverflow.com/questions/4427608/android-getting-resource-id-from-string) – kabuko May 31 '12 at 21:24

4 Answers4

3

I use this to get drawable id

getResources().getIdentifier(childImg, "drawable", getPackageName())
ChristopheCVB
  • 7,269
  • 1
  • 29
  • 54
1

use getIdentifier()

int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
seba123neo
  • 4,688
  • 10
  • 35
  • 53
1

Do it like this:

String childImg = "childIcon";
int id = getResources().getIdentifier(childImg, "drawable", getPackageName())
textView.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0
textView.setCompoundDrawablesWithIntrinsicBounds(Resources.getIdentifier(childImg), 0, 0, 0);
Pethical
  • 1,472
  • 11
  • 18