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

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.

Related

How do I load .class files into a variable?

I'm working on a text-based game as a practice project.
I already managed to write an engine that displays information and handles user input. It draws the games content from custom Scene classes. More precisely, I have a Scene superclass and create child-classes, like EvilDungeon extends Scene, for the actual playable levels.
All scenes are located in a "Scenes"-folder, but I want to be able to make game expansions later on by dropping additional .class files of new scenes into said folder.
My plan was to use loadClass when the game is started to add them to a Scene-array, but it requires the class names which I can't know, since there can a any random combination of scenes in the Scenes-folder.
How can I load all scenes in the folder without knowing which scenes exactly are present? Can I retrieve the class name somehow?
My plan was to use loadClass when the game is started to add them to a Scene-array, but it requires the class names which I can't know, since there can a any random combination of scenes in the Scenes-folder.
That's what Service Provider Interfaces are for. Just define an interface for your plugins and use ServiceLoader to get an instance of each plug-in.
The plug-in needs to provide a text-file in /META-INF/services named after the interface and containing the class which implements that interface (both names must be fully qualified). You'll find the details in the documentation of ServiceLoader.
This gives you the ability to add plugins to the class-path. E.g. you can run your application using something like java -cp myapp.jar;plugins/* my.app.Main, then all the plugin-Jars in folder plugin will be added to the class-path and are available to your application (for more information on the asterisk, see this answer).
If you want to load plug-ins at runtime, you can use a URLClassLoader, e. g. read this question or this tutorial on how to use the URLClassLoader.

Can't access classes in Portable Class Library

I'm new to mobile development and Xamarin. I am trying to make an instance of a class which is found in another project under the same solution (HotDog and HotDogDataService). I get the red underline the using saying The type or namespace name 'RaysHotDogs' could not be found.
Why can't I instantiate these classes in this project? The classes are public.
One of your projects is not being build, judging by the error message provided - in one of your layout files, if you look at the xml you probably have invalid property, It's one one of the relative layouts as far as I can see.

Organize resources in Java

How can I organize resources in Java?
For example, when I will use pictures to be embedded in my application, in what folder can I store it? Because when I make my program in an executable .jar file all the resources like pictures, text file, it goes outside the folder where I stored it.
A common practice is to reserve a package to resources. You can place it anywhere, depending on the classes needing to access those resources (there's no rule, it's just a matter of organization logic). For example, your class project.gui.Main needs to load some images, you can then create a project.gui.data package and store your resources in it. To load these resources from Main, use the following piece of code :
Main.class.getResource("data/img.png");
This way, you can access the resource with a local path. Never use absolute paths if the application si expected to be packed in a jar.

Play framework combining two Play application into one

I have two application written in playframework.
I would like to join one to another.
I have one database, and I would like to share my login classes between thems.
Applications have difrent names used for classes, methods, variables.
How can I achive it?
Should I create jar version and try to join them.
Or Should I move code from one app to another with new package name like application name
But what about shared views or main controller and configuration files
Please give me some advice.
One project make as sub-project of another one.
See this link:
https://www.playframework.com/documentation/2.1.x/SBTSubProjects

Custom Views and attributes used across Android apps

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.

Categories

Resources