Parse layout not from resources and set it in Runtime - android

I got a thing to think about, but cannot find the solution. New layout file is being received from the server.
I faced problem that Parser cannot resolve all those android attributes to inflate view in the next step. We can change file format on server-side to make it as attributes, but how to parse it and set properly still a question.
Does anybody have any idea or suggestions how to do that? Thanks in advance.

You can't. Two problems.
1)You can't parse a general xml file and make a layout from it. From the docs on LayoutInflater.inflate() "Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime."
2)You can't save to assets. Assets are read only. Of course you could use the filesystem instead.
You can get what you want, but you'd have to write your own parser for your layout files from scratch. Its highly not recommended.

Related

Why do we use xml language for layouts? [duplicate]

I would like to know why we use XML for the creating user interface layouts in Android. I know that it decouples the business logic from the design but what is the significance of the XML other than that?
Also I would like to know the significance of the auto-generated R.java file in this. All I know that it is generated according to the changes in the resources and that it helps us to access the widgets and resources through their IDs.
It would be great if someone could give a clear idea on these two aspects.
Unlike what everyone said about the XML being easy and efficient. Here is what I read in Hello Android by Ed Brunnette (p. 49) which made sense.
Android is optimized for mobile devices with limited memory and
horsepower, so you may find it strange that it uses XML so
pervasively. After all, XML is a verbose, human-readable format not
known for its brevity or efficiency, right?
Although you see XML when writing your program, the Eclipse plug-in
invokes the Android resource compiler, aapt, to preprocess the XML
into a compressed binary format.**It is this format, not the original
XML text, that is stored on the device.
This was the kind of answer that i was looking for.(sorry if my question meant otherwise).
The reason that XML was chosen is mainly because of its familiarity and the number of IDE tools that natively support it. The developers could have chosen JSON for example and still compiled that to binary.The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource.
XML is easy to parse and manipulate programmatically, it's basically a tree structure and most UI creation tools already use it. It really has nothing to do with decoupling business logic because you can define Java code in Android using a Model-View-Controller pattern just as well.
The auto-generated R.java file is a helper for the IDE so that you can get the benefit of autocomplete when you want to access a resource. It also stops you from making stupid mistakes since the compiler will complain if you try to access a resource you haven't defined. If you were using a simple properties file you wouldn't know until runtime that the 'key' you are using is missing.
Same as why is silverlight with xml the answer is simple xml give power by integration and scalability. R.java is for indexing having things organized is never bad.
Sorry my english
One possible reason is that you need not have any working java underneath in order to be able to see the visual layout of the interface you are working on. The xml ui element/page is essentially a document that you can parse and display. If this were a source file you would have to either carefully parse it or compile and run it (all of which are more complex than parsing xml)
Xml as itself is well readable both by human and machine. Also, it is scalable and simple to develop. Also, you have already mentioned the decoupling.
As for R.java - it is just the way of accessing widgets. The solution is rather good, as it is using ints instead of string to use less memory and in the same time provides well readable names for the simplicity of development.
Android Layouts are tree like structures with some enforced rules. XMLs are perfect fit for this purpose.
JSON also have tree like structure but they are data-oriented while XML is document-oriented.** :
Meaning XML is based on the idea that documents have structural and
other semantic elements that can be described without reference to
how such elements should be displayed.
The actual display of such a document may vary, depending on the
output medium and style preferences.
While JSON was designed to
represent JavaScript and their prime purpose is data exchange . They are well suited in data-oriented areas
because of light weight and simplicity & closer in syntactic form to programming data structures.

a parser application which can implement xml files and run accordingly

I want to implement an application which will work as a parser. User will be able to upload xml files which will describe UI and functional properties of some android application. I wonder if there is already a way to parse xml files and use them as layouts without compiling?
I think that you're parser will have to generate new Views which receive "parameters" via the xml files. Afterwards you'd have to add the Views progammatically to your layout root.
It doesn't really matter which parser technology you are using for this purpose as you're going to create objects from it - so SAX, DOM & Pull-Parser would all be suitable.

Storing data in pre-defined XML files

I'm trying to figure out how to store data in pre-defined xml files (the files should be part of the project).
I've tried to several methods but non worked.
Instead of asking what is wrong with what I'm doing, I rather ask what is a good known working method to do that.
Thanks.
There are plenty of tutorials about parsing xml. Try those by IBM. Just place your xml in/assets and parse it. But I believe newest api has even more convinient way to do it. Refer to dev guide. As fo xml itself just follow common standarts.
http://www.ibm.com/developerworks/opensource/library/x-android/

Avoid XML convertion in .apk to .apk.zip in android

I create an application in android.
I know .apk can convert into .zip and my layout and xml files are extracted.
Is there any way to avoid the decoding of xml?
How can i encode the apk so it cannot converted?
thanks
An .APK file is simply a .ZIP file with a predefined directory/file layout, there is no "conversion" (unless you're simply talking about changing the extension).
ProGuard can be used to obfuscate code, but there's no "standard" way of encoding/encrypting the XML files. You could encrypt/decrypt them manually yourself, but you'd be introducing a great amount of overhead at runtime for decryption, and many standard interfaces would not work because they expect the standard plan-text XML format. You're looking at a great deal of code to accomplish what you're asking. This is all assuming you're loking to encrypt your strings.xml, etc.
If you're looking to encrypt custom XML data files, there's a thread with some good suggestions here.

Parsing a XML file in Android

Can anyone provide me with the info to parse an XML file in an Android application.
Any link on this will be helpful.
There are 3 different ways to parse XML in android: SAX parser, DOM parser, pull parser. Which one to use depends on how big your xml is, and what you want to do with it. See working with xml in Android for details. (Which, incidentally, is the 1st link if you google "Android XML"].
Parsing XML in android is done in more than one ways in android..
Android.developer is the best reference that you can go for.
you can ieither use SAX or DOM..
Here is the link which will give u idea about parsing XMl
http://developer.android.com/reference/javax/xml/parsers/package-summary.html
go through the documentation first it will help u a lot
You can find a great explanation at this link.
You basically use the dom4j SAXReader to parse the XML using XPath.

Categories

Resources