Android R.* references in appcelerator module
Hi folks, I've asked this question already in your google group but didn't get any response. So I thought I'll try it here:
I'm trying to port an existing barcode-scanner module for android to
appcelerator. Unfortunately there are some R.* references to XML ui-
definitons, internationalized strings, drawables and colors. I haven't
figured out if there's a way to use them inside of an appcelerator
module. And if there's a way I haven't figured out how to use them.
Is there any way to use those resources inside of an appcelerator
module?
If not:
- Is there a "right" way to put drawables to the module so I can get a
reference of them inside my code? - Is there a way to do i18n inside a module?
- Is the only way to create ui-definitions for activities to write
them down in Java-Code?
I'm a bit stuck at the moment so I thought someone could give me some
advice about what to do or where to look for clues.
Thanks,
pfleidi
4 Answers
-
I had the same problem and it seems to be have been solved by getting the resourceId another way. It must be looking for resources in a different directory/package or something like that.
getResource() gives an Error
getAndroidResource() gives an Error
but getApplicationResource() DOES work
findViewById( TiRHelper.getApplicationResource("id.image_view1"))
-
Sven, I'm looking at the same kind of problem. I think it should be possible but seeing as modules are typically built without any knowledge of the app that will go and use it I believe we should be making references dynamically, maybe through parameters to our modules. But I would be interested in finding out what the correct approach is.
-
Same Problem here.
I got my ResourceID of an XML-Layout in my module-directory/platform/android/res, but findViewById() results in a null object.
Any solutions now after 2 years? :-) -
I finally got my R resource references to work by referencing them as strings in my module's native code, and dropping the resource files into my $APP_DIR/platform/android/res directory. NOT my module directory, or any directory within it.
You could probably pass the Strings to the module's kroll method from your JS code. From JS, Ti.Android.R gives you access to the resources somehow, but it seemed like too much hassle so I just hardcoded mine into the module, since I knew what I was putting into the app.
If you called a Kroll method from JS, for instance myKrollMethod(Ti.Android.R.drawable.myicon), then when you looked at what the Java code in the module received, you'd have been passed an RProxy object. From that, you can extract the resource's name.
Once you have the name, from within the module you call:
TiRHelper.getResource(String resourceName)
This will return the integer ID of the resource you want from your application's R class. Again, NOT from your module's R class.
Here's a quick sample:
import org.appcelerator.titanium.util.TiRHelper; //..... //Blah blah code //..... //Get the ID of R.drawable.myicon //This could possibly be extracted from an RProxy object String icon_name="android.R.drawable.myicon"; //This is where we will store the ID from R int icon_id; try{ icon_id=TiRHelper.getResource(icon_name); } catch(ResourceNotFoundException e){ System.err.println("Failed to locate required Android resource \'" + icon_name + "\' : " + e); System.exit(1); }
Of course, if you want to understand this better you'll need to read the Titanium source, since Appcelerator have done what I would call the singularly worst possible job of documenting their product. In fact, I'm going to use something else for my cross platform development in future, and recommend that you do too.
If you do read the source, beware that it has next to no comments in it, at least for the Android related stuff.