Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked August 4th 2010 by Sven Pfleiderer
1 Comment
  • Hi, I share as I solved,

    answer similar question

    — commented January 7th 2014 by Alejandro Alfonso Oropeza López

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"))
    
    — answered August 2nd 2013 by Leon Schreuder
    permalink
    0 Comments
  • 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.

    — answered August 4th 2010 by Charlie Roche
    permalink
    0 Comments
  • 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? :-)

    — answered July 23rd 2013 by Marcus Biel
    permalink
    0 Comments
  • 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.

    — answered January 31st 2011 by Ewan Sinclair
    permalink
    2 Comments
    • Hi Ewan,
      i read your example and i tried it: it works, i can get the id of the resource but i can't get the resource itself, 'cause i tried to get it with:

      // it works
      int myViewId = TiRHelper.getResource("id.my_view");
      // it doesn't work
      myView = (TextView) findViewById(myViewId);
      

      i got a null object…i think i need to use the TiActivity.findViewById but i don't understand how to do it…can you give me an advice please?

      Thank you very much, regards.

      — commented March 8th 2011 by Matteo Traina
    • i get Error generating R.java from manifest, can you help me? this happen when i add res forlder and put my xml file inside it
      invalid resource directory name: C:\Users\Titanium_Studio_Workspace\myapp\build\android\res/activity_main.xml

      — commented December 3rd 2013 by Ario Barzan
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.