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.
Related
I have a requirement where different views are to be shown on the same screen depending on the requirement. Which approach is better adding respective XML files or writing in java code by using TabActivity class object?
Short answer, defining your layout in code is better for performance than using XML.
Inflating an XML layout involves doing the same work as creating the layout in code, however you also need to parse the layout file (at least the first time it's used) which adds work compared to doing it in code.
HOWEVER, for the majority of use cases the performance hit is not noticeable, and the simplicity of defining an XML layout outweighs the performance benefits of doing it in code.
IMO, Writing layouts file is a better option.
Since, Writing layout will let the view load on compile time.
Whereas, While creating views pro-grammatically leads to slight increase in Runtime operations of the app.
Code written in java means dynamic generation of views are faster than xml rendering as the views increasing. Xml prefers as it is bit easy to implement the design using xml but it gets tough to handle when we use java to create dynamic views.
In your case if you have requirements to create dynamic view then you can create with Java.
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.).
My Android app has two layouts, one for <= normal, and one for >= large. I can re-use my corresponding activity 99.9% in both cases, but I need it to know different bits of information for the two layouts. I could of course write a giant if expression, but I'd rather let Android work its magic, since it's already doing it by loading the two different layouts.
So, can I embed pieces of information in the two XML files and retrieve them in my activity class? Or am I completely off the map and the right approach is completely different?
Sure you can, just in the values directory define values for each size and retrieve them dynamically in your program.
/res/values-xxx
-> a.xml
/res/values-yyy
-> a.xml
...
here is an example:
<resources>
<integer name="maximum">100</integer>
...
</resources>
in your program just put:
int max = getContext().getResources().getInteger(R.integer.maximum);
for each size android will magically do the job and give you the correct value!
If you're willing to go the custom View route, then yes, you can. What you have to do is create custom attributes and apply them to your custom views which are parsed when they are created. Here is a thread that goes in to a great bit of detail about it.
The Views themselves don't have to be special. You can say, have a view called a PropertyView view which extends FrameLayout and has a method called getProperty(). You then put the property in the XML like so:
<com.example.ProperyView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:property="My Custom Property"/>
Then you would parse it using the methods described in that link.
EDIT:
Alternatively, there are other elements in the XML landscape that can be put in to buckets similar to how Layouts are. Any folder in the /res folder can have the same buckets that the Layouts can. That includes anything in the values, drawables, raw, or xml folders. You can reference these in your Layouts and the Android system will pick which ones you want. More example for how dimens work.
If you are using values to differentiate between the two layouts, then you can have different values that overload depending on screen size. See this answer.
You could do the same sort of thing with the layouts directory to create different layouts, but then use common subsections using the < include > tag to make the different views based on common sections.
Or a combination of the two. If you the want to change the behaivoir of the Activity/Fragment, you could key that on the presence of missing or present screen widgets.
Im working at a little Annotation Processor for android.
I have the following scenario:
I have a xml layout resource id and I want to find all views in this xml layout. I simply want to parse the xml layout file to retrieve some information that I will use later on.
Does anybody know if such a tool exists or how to implement something like this?
I know it's not simple. There are serval things to consider like:
same layout files in different layout-resource folders like:
res/layout/mylayout.xml
res/layout-xlarge/mylayout.xml
I want to parse both mylayout.xml files.
I only have the resource id (integer) of a layout, how do I map that back to the xml file (String, name)
Any suggestion how to start?
I doubt I can use Android classes, because I want to write an Annotation Processor. AnnotationProcessing runs in it's own jvm before compiling Android resources.
From what I understand the workflow should be as follows:
Map id (integer) to layout file name (String). I guess I have to
parse the R.java class to achieve that.
Next I have to check
recursively all layout resource folders to find the corresponding
layout.xml files.
Parsing the xml files (no big deal)
if you need the name of the resources that correspond to a particular id, you can use
String getResourceEntryName(int id)
that is a method of Resources(). Here you can find the documentation
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