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
  • 21,137
  • 63
  • 182
  • 307
  • 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,181
  • 1
  • 28
  • 54
1

use getIdentifier()

int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
Juan Cortés
  • 19,778
  • 8
  • 64
  • 88
seba123neo
  • 4,571
  • 9
  • 33
  • 51
1

Do it like this:

String childImg = "childIcon";
int id = getResources().getIdentifier(childImg, "drawable", getPackageName())
textView.setCompoundDrawablesWithIntrinsicBounds(id, 0, 0, 0);
waqaslam
  • 66,380
  • 16
  • 161
  • 174
0
textView.setCompoundDrawablesWithIntrinsicBounds(Resources.getIdentifier(childImg), 0, 0, 0);
Pethical
  • 1,402
  • 9
  • 17