How to use reusable gui element / widget with resources in android

I am trying to use dateslider in my android project to have a combined time set to pick a date in my gui.

The authors of the widget suggest including java and resource sources directly in my application.

First attempt to copy java and resources to the main application:

Result: The widget java classes cannot compile because the Rxxxx resource cannot be resolved.

Reason: Widget classes are implemented in the package "com.googlecode.android.widgets.DateSlider" and my application has a different namespace "my.namespace.myApp", so resources come from my.namespace.myApp.R.xxx.

To fix , I would have to touch each source of the javas widget to import my.namespace.myApp.

Is there a way to have 2 sets of resources with different namespaces so that my.namespace.myApp.R.xxx and com.googlecode.android.widgets.DateSlider.R are in the main application?

The second attempt to put the java + resources widget in a separate jar / library:

result: all compiled. but after appstart i get runtimererror: runtime cannot resolve widget resource ids from jar / lib.

Note. I can call methods from the bank if they do not need resources.

So my question is: what is the best way to use a reusable gui element with resources in android?

I am using android 2.2.

Note: Android: how do I create reusable components? doesn't help, because it tells you how to create library projects.

update 16.3.2012

Since the current version of 16/17-pre from eclipse adt-tools does not support jar resources (like from try 2), what is the best / easiest way to use them until there is support for this?

update 4.4.2012

With the new R17 tools, I was able to use library projects with the resources that the bank creates. Android-Lint helped me find out what needs to be changed in lib to make it usable.

  • eclipse-workspace

    • DateSliberLib
      • CSI
      • Res
      • ...
    • MyAppUsingLib
      • CSI
      • Res
      • ...

    MyAppUsingLib is working fine with this location


However, I still can not use only DateSliberlib.jar

  • eclipse-workspace
    • MyAppUsingLib
      • CSI
      • Res
      • Lib
        • DateSliberLib.jar
      • ...

This parameter can be compiled, but the application crashes because it cannot find lib resources.

[update 2014-11-17]

6 months ago, I switched from eclipse / ant -build to the android-studio / gradle assembly, which included * .aar files, which are jar files with android resources.

android-studio / gradle build can handle resources in libs.

+1
android resources android-widget
source share
1 answer

Is there a way to have 2 sets of resources with different namespaces so that my.namespace.myApp.R.xxx and com.googlecode.android.widgets.DateSlider.R are in the main application?

No, sorry.

Note. Android: How to create reusable components? doesn't help, because it tells you how to create library projects.

However, this is the correct answer. Download the full project from your repo, import it into Eclipse and mark it as a library project (Properties> Android). Then add it to the library project in the application project.

In the end (now it looks like version R18 or later), the tools must support the packaging of reusable components in the JAR with resources so that you can add them to the host project and the resources will be merged automatically. Right now, this is not an option.

+1
source share

All Articles