Android resource practices for libraries - android

It is already well established that dimensions, colors, and other resource related values should be stored in their respective XML file. However, is this still appropriate when creating a library? Defining these resources in a library's XML file would perhaps bring confusion to anyone using the library. Are there are recommended practices for defining resources in a library project?

yes one giant one .. prefix the resource name both in the id and the xmlfile name as resources with same names often clash with parent stuff that has the same name

Related

Is it possible to use an arbitrary name for an XML resource file in Android Studio?

I'm attempting to create a 'Preferences' Activity for my Wear OS app (home-baked as I don't believe the standard Settings Activity copes with round screens).
In order to support the round screen I am planning to use a WearableRecyclerView and so need to define string-arrays for the contents of the Recycler layouts.
To keep things clean in my code, I'd like to keep these string-arrays out of my strings.xml files if possible.
Therefore, is it possible to use, for example, preferences.xml in the res/values folder (and provide translations in the values-?? folders) and then reference this in code?
I have tried creating preferences.xml but when I try to retrieve the arrays with
String[] prefsTitlesArray = getResources().getStringArray(R.preferences.prefs_titles);
I get an error flagged in the IDE as 'preferences' isn't recognised under R.
Do I have to stick to the standard .xml file names such as strings.xml and array.xml or is it possible to use an arbitrary file name under the values tree to keep thinsg nice and clean and obviously named?
(Note, I have looked at Is it possible to create translateable arbitrary XML resources in Android Studio? which seems to imply that arbitrary xml file names might be possible outside of the values tree, but doesn't mention how they are referenced in code (Java, in my case).
As per Mike M's comment, yes it is possible to name the XML resource files anything you want as the code reference R.????.itemName is derived from the item type not the file it comes from.
So a file called prefs.xml could contain <string name="itemName"> items and <string-array name="itemName"> items etc and they will be referenced from code as R.string.itemName and R.array.itemName.
The XML filename itself is irrelevant so long as it is saved in the correct folder within the project for value resource files.

Loading XML layout from server pros and cons?

In order to avoid frequent updates is it a good practice/is it possible to load XML layout from the server? So any necessary changes can be done on the backend without having to update the app.
From the docs:
When your application is compiled, aapt generates the R class, which
contains resource IDs for all the resources in your res/ directory.
For each type of resource, there is an R subclass (for example,
R.drawable for all drawable resources), and for each resource of that
type, there is a static integer (for example, R.drawable.icon). This
integer is the resource ID that you can use to retrieve your resource.
So, layout files are compiled (in a binary-like format) as part of your project. If the XML was not part of your project at build time, you won't be able to use it during run-time.
is it a good practice/is it possible to load XML layout from the server?
No because of what I wrote above.
to do a native vs runtime/remote layout inflation depends on your use case. (so I will not touch on whether it is good practice or not).
as to how to accomplish a runtime layout inflation -> you can use https://github.com/flipkart-incubator/proteus as a replacement for your layoutInflator
from the page it states that
Instead of writing layouts in XML, in proteus layouts are described in JSON, which can be used to inflate native Android UI at runtime. The JSON layouts can be hosted anywhere (on the device, on servers, etc.).

android values folder item naming convention

I've come across some android native code where the strings.xml in android would be named strings-global.xml and likewise the styles.xml would instead be called styles-global.xml. how is android identifying this ? what does it mean ?
The naming of files within the values\ folder is arbitrary. What resources the file provides is defined by the contents of the file itself, not the name. Which resources are used in a given configuration is determined by qualifiers on the values\ folder (e.g. values-fr), not the file name.
You could even mix and match resource types in a single file if you really wanted to, though I recommend following convention.
From the Providing Resources documentation for the values\ folder:
Because each resource is defined with its own XML element, you can
name the file whatever you want and place different resource types in
one file. However, for clarity, you might want to place unique
resource types in different files.
In this case, it sounds like the developers discovered that their styles.xml was getting too large and/or they found that they had a few distinct categories of styles that they wanted to separate, and thus put them in different files to keep them more organized.

Android Resource ID

Must the resource ID's for views in XML layouts be unique across all layouts?
For example, I'm working on a little recipe manager app. I have a layout file for adding a new ingredient. In this layout I have an EditText for the ingredient that I'd like to call "edt_name". But I'm afraid that this name is too general; e.g. I might also have an EditText for a recipe name, a cooking procedure name, etc in other XML layout files.
However, I also don't want to make the labels more complex than necessary. I'd like to avoid calling the aforementioned EditText "edt_name_new_ingredient" if I could.
I'm curious as to how developers organize their resources in general. Android doesn't support sub-directories for resources as far as I know, so naming schemes can get really messy.
No, resource ID should not be unique across different xml layouts however they must be unique in a particular xml file.
Resource IDs are namespaced within the package. When you access a resource (in XML, for example), the package name is implicitly set to the current one. You can have other resource files in a different package and refer to those within your code or XML (this is how one accesses the platform resources that come with the SDK).
Similarly in code, you can access a different package's R class and use its resources, but all those within the same package must have unique names.
More info can be found in the documentation here.

Resource referencing in android

I am newbie to Android and playing with some Hello World codes.I observed that android put every resource i.e. image,string etc in res folder and we access it like #drawable/icon i.e icon image in drawable folder or like R.layout.main which means main.xml inside layout folder.
But while accessing strings we use #string/string_name but we dont specify its parent folder name i.e.values.Why syntax differs for strings ?
It may sound silly but it makes to think and put this question.
All resources of the same type in android is in a flat hierarchy.
You don't specify a directory name but instead the type of the resource.
Even if you split all strings between different files it will always be #string/string_name I'm afraid.
A good thing you can do to get some structure is to do something similar to this:
#string/error_network_io
#string/error_network_unknown_host
#string/message_save_successful
...

Categories

Resources