0

I'm trying to implement a way in which a layout id string is being given to the layout inflater instead of using it as and id integer, this is important since i want to be able to use a dynamically built id.

i've tried using the getResources.getIdentifier method like so :

View view = inflater.inflate(getResources().getIdentifier("R.layout.coollayout", "layout", null),null);

however the getIdentifier() function keep returning 0 which in turn causing the app to crash, what am i missing here (and yes i'm sure i have a layout with that name)?

Liran Cohen
  • 1,190
  • 1
  • 9
  • 16
  • You usage of `getIdentifier` seems incorrect to me, see http://stackoverflow.com/questions/15488238/using-android-getidentifier for example –  Jan 31 '15 at 16:00

1 Answers1

0

Should be

getIdentifier("coollayout", "layout", context.getPackageName()) 

Context is, for example, Activity.this or getActivity() for a Fragment. Not sure, maybe null as the 3rd arg will also do.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • thank you, this worked perfectly! however im curious of the logic behind this syntax, whats the package name has to do with resources anyway? the package as far as i understand it is just for java classes, the resources are independent of the packages and can be used in any class in any package as long as they are in the workspace? – Liran Cohen Feb 01 '15 at 20:14