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.).
Related
I'm developing a generic Android application that needs to get XML files from the server and set its contentview accordingly.
The XML files can be as simple as a relative layout with a textview or much more complex, that's really not relevant. The goal is to simply fetch XML files externally and display them or receive a string from the server, create a XML file locally on runtime and use it, both will do.
I've been looking for a while now and I didn't find any solution. Is there a way to do this?
According to this post which references Android's LayoutInflater doc; it is not possible since there's some "pre-processing of XML files that is done at build time."
In short, you can't use a regular xml pulled externally to inflate.
I have a list of strings (messages and urls) which I want to place in a common file. Would it be more efficient to place them in a container class or as an xml resource?
If I want to use the strings in my xml layouts, I guess I have to put my string in an xml resource?
Edit: I know the advantages of both approaches, all I want to know is which is faster.
XML resources are actually compiled to binary form when you create your APK, so they are pretty fast. If you use resources, you can define different string for different languages (locales), versions or screen sizes, and the platform takes care of using the right one automatically. There is no reason to use a container class, use XML resources.
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.
I thinks it's probably not possible for security reason but just want to be sure: is it possible to create layout in Android from an external XML file?
To be exactly: I'm creating XML output with XSL on a remote server. I could create the necessary layout direct on the server and then download it to my Android App.
It is impossible. XML layouts in Android are NOT stored as XML. For performance reasons, they are pre-processed during compilation and stored in binary form, and layout inflater only understands that binary form rather than xml.
Of course you can create Views dynamic at runtime, while I'm not shure, that this is the best solution. If you have a look at the internals of Android, every View which is created through XML is called with a Constructor with two parameters: Context and - even more interesting for you - an AttributeSet. I think you have a lot of work with parsing it, while keeping track of the right format.
You could at least set the values and build your views yourself in Java depending on Server output.
YES, right now is possible with ItsNat Droid, take a look:
https://groups.google.com/forum/#!topic/itsnat/13nl0P12J_s
It is still under heavy development but most important features are already implemented.
it's possible, but I found way to load only simple layout:
Resources resources=context.getPackageManager().getResourcesForApplication(targetPackage);
int resID = resources.getIdentifier("widget_layout" , "layout", TARGET_PACKAGE);
XmlResourceParser parser = resources.getLayout(resID);
View themeLayout = LayoutInflater.from(this).inflate(parser, null);
Maybe the inflate function of the LayoutInflator works for you.
EDIT: doesn't work yet it seems.
I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
http://developer.android.com/reference/android/view/LayoutInflater.html