Custom Views and attributes used across Android apps - java

I've developed several Android apps now and have created a code base of classes that I frequently use in more than one app. This code is all in a subversion (svn) repository with each app in its own repository. Each app then has svn:externals references for the needed packages (e.g., com.company.android.views). This works great except in the case when the R class has to be imported for custom attributes.
A custom view has an import like this:
import com.company.apps.myapp.R
so that it can have code like this:
attributes.getBoolean(R.styleable.WebImageView_autoload, autoload)
That custom attribute is defined in res/values/attrs.xml:
<declare-styleable name="WebImageView">
<attr name="autoload" format="boolean" />
...others
</declare-styleable>
This works perfectly, but the problem comes when I have a second app that uses this same view. Now I update the import to import com.company.apps.anotherapp.R so that it will work with "anotherapp" and that breaks it with "myapp." When working on several apps at once, this becomes an issue.
My temporary solution has been to check in an update to the applicable classes and then lock the svn:externals to that specific revision. Each app ends up being locked to a different revision, which gets messy fast, but that still seems better than copying the various classes into the app's repo directly.
The only other solution I've thought of it using reflection, something like:
Class class = Class.forName(context.getPackageName() + ".R");
Field[] fields = class.getDeclaredFields();
And then loop through the fields, assigning the ones I care about to variables that are used throughout the class. This seems rather heavy-handed though, especially when we could be talking about several classes needing to do this.
How can I solve this issue? Is there a way to dynamically import the com.company.apps.*.R or to somehow generate a different R class that doesn't depend on the specific app? Or is there some other obvious (or not so obvious) solution I've totally missed?

Took me a while, but I found a good answer: Library Projects.
Structurally, a library project is similar to a standard Android application project. For example, it includes a manifest file at the project root, as well as src/, res/ and similar directories. The project can contain the same types of source code and resources as a standard Android project, stored in the same way. For example, source code in the library project can access its own resources through its R class.

Related

An asset packaging tool for plain old java (like Android's R class)

I was impressed by the way android creates an R class during development. I do my development in Android Studio (which basically is a tuned IntelliJ version). Whenever I add a resource, in a matter of seconds the R class is updated with new fields.
When I develop in my plain old java projects, I am having the same desires. Instead of creating many public static final String declarations. I would like to have something like the dynamicly created R class. For obvious reasons of course: less chance on typos.
I started to wonder how they achieved this, because just like in Android development, I have a java project with many resources (i.e. String declarations) in custom XML files. I would love to setup something which processes my custom XML files and creates a dynamic class during development.
The javadoc of Android's generated R class reveals that it was generated using the AAPT tool. (which stands for Android Asset Packaging Tool). So, I am wondering if something similar exists for plain old java projects.

Android package names

In an Android application, I notice that the package name of a class does not have to match the name that was generated when I created the new project. In other areas, Java is insistent that everything matches. I would like to understand why this is not the case for package names, and what the implications are.
Example: I create a new project named TextToSpeech, using my own reverse domain name. This gives a package name of com.lexogram.texttospeech; the path to the MainActivity.java class file is TextToSpeech/app/src/man/java/com.lexogram.texttospeech/.
I now go to a TextToSpeech tutorial and copy-and-paste the code into my project. This code uses com.example.texttospeech as the package name, both in the MainActivity class and in the AndroidManifest.xml. I run the project, and everything works fine.
Does this mean that each activity can use a different package name, so long as the name is used consistently across all files in the activity?
What it means is that your project can have multiple packages inside of it.
Java documentation explains them nicely:
To make types easier to find and use, to avoid naming conflicts, and
to control access, programmers bundle groups of related types into
packages.

Scan for classes in namespaces which arent imported yet

Is it possible to make net beans scan for classes in namespaces which i haven't imported YET.
What I mean is, when I begin typing the name of a class, netbeans doesn't show it in its 'code prediction'. it only shows after I have imported the class's namespace.
I want it to show the class in the code prediction regardless of if I've imported the namespace or not
This isn't an issue with eclipse, can I make it work like eclipse?
Please go through this link.. I have followed this and I achieved what you are trying to get. https://netbeans.org/kb/docs/java/editor-codereference.html
Moreover, you can always customize your code completion settings to whatever behavior you need. It behaves almost similar to eclipse.

Unable to import image resources into our GUI

I'm working on a project with some friends over Github for a University project. I've only just taken my friends code off the repository for the first time. For some reason, all references to images in the code don't seem to allow compilation due to a directory problem I think. I'm using Eclipse, my friend's using Netbeans(don't know if that affects it or not?). Anyway, all of the images referenced are either in a folder known as runone, on the same level as the Eclipse src, or within the package 'runone' within src. I don't know which.
Here's an example of some of the references:
jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/OSTeeeennny.jpg")))
jLabel53.setIcon(new javax.swing.ImageIcon(getClass().getResource("/runone/clown_fishTIny.jpg")));
I guess what I'm wanting to know is, how can I make these resources work correctly, on any machine that we code this program on?
Hope that all made sense!
For the examples you have given your file structure might look like this
src/com/yourpackage/YourClass.java
src/runone/OSTeeeennny.jpg
src/runone/clown_fishTIny.jpg
For a more best practice way of organizing your resources you could do this
src/com/yourpackage/YourClass.java
src/com/yourpackage/resources/OSTeeeennny.jpg
src/com/yourpackage/resources/clown_fishTIny.jpg
and then use the following calls to load them
getClass().getResource("resources/OSTeeeennny.jpg")
getClass().getResource("resources/clown_fishTIny.jpg")

Howto access the main res/layout/ files from a sub package?

This is also a design question. I don't know the best way to design having many activities and how to break them up into packages while still accessing the apps resources.
I have an application which allows a user to login and access a tabbed activity which gives access to many modules. If they click a module they will load the module which will in turn contains a tabbed activity.
Currently i have 3 packages within this app:
com.appname.app (many activities i'd like to split up, login, module list etc)
com.appname.app.XML (xml handlers)
com.appname.app.Utils (static util classes etc)
I'd like to add a new package to contain all my modules so something like
com.appname.app.Modules ( or even a seperate package for EACH module)
But i can then no longer access the main apps resources without referring to them with a full package name such as setContentView(com.appname.app.R.layout.channel_list);
Obviously within any com.appname.app activities i just reference the resources such as layouts via setContentView(R.layout.channel_list);.
I'd have thought that a sub package (sub directory) should have access to a parent's resources without having to use com.appname.app.R.layout.channel_list instead of R.layout.channel_list. Which is why i'm assuming something is wrong with my design
The same issue goes for accessing string, drawable etc resources...
Im sure this is something very basic I'm missing. I've tried manually importing the "parent" package and that didn't work either.
is this how i should be doing things? or should i somehow be creating the resource files within each module package? or there another way i should be handling the design/split up of my app?
Since the Android pre-compiler would compile all references to a R.class file and place in in the main package there's no better way than to explicitly import the resources wherever you want to use them
import com.appname.app.R;
so that no full qualifier is needed.

Categories

Resources