Monday, October 24, 2011

UI IDs in Android library projects, with ADT 14

We just moved over to the newest version of ADT (version 14), which substantially revises the way library projects are handled in Android.  The impetus behind the changes are a good thing -- package everything into a jar file instead of a shared source folder.  This should mean that in the future we'll be able to easily treat our library folders as a binary when we need to provide source to someone.

So far though, this is a bit bumpy.  The big thing is that we have a lot of UI work happening in our library projects.  Things like color pickers, display of color swatches, slider bar preferences, and so on are all shared between something like 50 different projects, and all of them are contained in a central library project.  Obviously this avoids a massive amount of code duplication.

The problem I'm finding is that every time one of these library-based UIs gets used, it fails as soon as I try to select an element by an ID.  I assume the ID isn't valid, maybe as a side-effect of IDs no longer being static?  Either way, something like this still compiles fine and nothing has changed with the layouts or anything:

        LinearLayout layout = (LinearLayout)inflator.inflate( R.layout.preferencecolor_layout, null );
       
        TextView main = (TextView)layout.findViewById( R.id.label_main );
        main.setText( getTitle() );

Except that while previously main.setText worked fine, now main ends up being invalid.  I'd be surprised if they actually broke the ability to do this at all, so suspect I'm just missing something.

Update: Actually, this works fine.  Somehow or other in the process of messing with this, something in my library folder never actually got recompiled.  I'm confused about that since I did adjust the syntax a couple of times, but maybe since I didn't change any resources R.java was never recreated, and was now invalid?  Either way eventually I cleaned the project and now everything seems to behave properly.  Works for me.

No comments:

Post a Comment