Is there a way to dynamically get styled attributes?
For instance, to find a resource id, I can use:
context.getResources().getIdentifier("idtofind", "id", context.getPackageName());
Is there something similar to this to get styled attributes?
A little background...I have a library which has a custom sliding drawer. It has custom styled attributes in the project's /values/attrs.xml.
I got it to drop into the UI in the GLE, but it doesn't show up, because it can't find the ids for the handle and content. The line is this:
TypedArray a = context.obtainStyledAttributes( attrs, R.styleable.DrawerSlider, defStyle, 0 );
I'm not sure how to achieve the above, as the styleable is an int array, so getIdentifier() won't work.
EDIT:
Ok, I've been able to get things to work in code fine now, but still having problems with the GLE. I'll try to clarify a bit more now. Like I said, this is for a library, which is freely available.
After testing, I've discovered that using getIdentifier won't work (it always returns 0, unless it's running the app ). Right now, I'm manually parsing the AttributeSet to get the ids of all the attributes I need (handle, content, direction, etc). When the GLE processes this, it returns +id/handle, rather than the resource int representing it.
It seems the only thing that will work for the GLE is to directly feed it the R.id static int. Seeing with the newer versions of ADT (I'm on r17) you can't reference the library's resources, I don't see a way to dynamically feed the resource ids I need.
Right now, it seems the only way to get this to work is to place the source file of the drawer itself in the user's project, which I would really like to avoid if at all possible. It would mean either placing it in the project's source dir upon creation of a new project, or adding a right click submenu command to place it. Both just seem like bad workarounds.
There isn't really anything special about the styleable array. All
obtainStyledAttributes()
does is allow you to obtain a batch of attributes in one call, instead of having to perform a separate call for each attribute, which is slower. So the styleable array is just an array of resource identifiers for attributes. There are special requirements on how it is structured -- the resource identifiers need to be in sorted order, as this is part of the optimization to quickly retrieving them. So if you want to build your own array, you need to make sure that the identifiers you put there are appropriately sorted. If you don't know the identifier numbers, you can use the code you showed to get the value for each one... though note that doing all of that setup each time you callobtainStyledAttributes()
probably more than removes the performance gain you would get from the function.You could try