Deserialize Android Layout XML? - android

Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code? Does a tool like JAXB work and if so how do I use it?

It's an interesting idea, though a bit useless IMHO. Why would one want to do it? Inflating an XML is very fast operation and hides a lot of complexity - for example it takes into account what is the current DisplayMetrics and recalculates layout parameters (width/height) appropriately to the density and size of the screen... It's also very fast because it does not actually require XML parsing - parsing is done at compilation time and what is actually stored by android is a binary version of the layout which is optimized for efficiency (that's why you cannot build layout XML dynamically from an XML file).
If you would like to modify the Java code and add/remove some elements then it is much more efficient to inflate the XML and then do all the modifications -less clutter simpler code and all the calculations are done for you...

Whats the best way for me to take an android XML layout file and automatically generate the equivalent Java code?
Step #1: Parse the XML.
Step #2: Generate the Java code. You'll have to pray that you can build your own AttributeSet implementation that works with your generated code, otherwise this will be a very complex problem.
Does a tool like JAXB work
AFAIK, JAXB needs an XML Schema, and there is no such schema possible for the Android layout file.
Whatever problem you're trying to solve this way, there's probably a better solution.
I just want a simple way to take an android xml layout and pass it to, say, a command line tool to generate the equivalent Java.
I seriously doubt that this will be "simple" for any reasonable definition of the term.

Related

Is there any performance difference between defining layouts in XML and writing the same in java code?

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.

Setting layout by remote xml

I was wondering if it was possible to set a view through a remote xml file. I had a look on the web and I found this post here on stackoverflow. Reading the answers I got I can't do it.
Why?
XML layout files are pre-processed at build time in order to provide efficient inflation of complex layouts.
Although there are LayoutInflator methods which take a path to an XML file, they have never been implemented.
In other words, unless your XML layout file is pre-processed and packaged at build time into your APK, then it can't be done.
There is one possibility, however, you could build an XML parser to parse your 'external' XML layout file and create your layout dynamically using Java code - not impossible but you're pretty much on your own if you choose to do that.

Using XML in Android for development

Hi,
How to use XML code instead of Android code to be used regarding UI development? In what manner XML is different from ordinary Android code?
Basically, if you've static elements in your layout, it's better to use XML. If your layout has a property to change itself dynamically, then it's better to go for Java. When it comes to usefulness as such, using XML is a bit better because you can easily re-configure your XML when compared to the dynamically running code. Finally, designing is a matter of choice and opinion.
Its much easier to develop UI using XML than by JAVA code mainly due to visual Drag-and-Drop feature. But by XML way of laying out views is Static in Nature but by Java code it can made to work during Runtime(may be adjusted to different conditions). So it is recommended that part of the layout which you think, undergoes no change in time should be implemented by XML and vice-versa by JAVA coding. Also static UI by XML can be overrided by JAVA cod during runtime as per requirment.
Read this ,you will get your answer :
http://developer.android.com/guide/topics/ui/declaring-layout.html

How do I get about adding something to my Android Layout?

I am a complete newb to Android and have seen tutorials that show widgets being added via XML and also via Java. I want to know what industry experts prefer and why. The author/narrator seems to prefer XML saying it gives more control but I wanted to get feedback from veterans.
With XML Tutorial
Without XML Tutorial
Thank You and again I apologize for a complete newb question.
XML layouts are very easy compared to java code. The coding also very less than java.
You can prefer any of these two, but all authors prefer XML because its easy.
Please read information available at this link, you will get an idea.
XML Layouts
Declare UI elements in XML (most
common and preferred)
Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses,
such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.
Instantiate layout elements at
runtime (programmatically in Java code)
Your application can create View and ViewGroup
objects (and manipulate their properties)
programmatically (in Java code).
Advantages of : Declaring UI in XML
• Separation of the presentation from the code
that controls its behavior
You can modify UI without having to modify your
source code and recompile
For example, you can create XML layouts for
different screen orientations, different device screen
sizes, and different languages
• Easier to visualize the structure of your UI
(without writing any code)
Easier to design/debug UI
Visualizer tool (like the one in Eclipse IDE)
Advantages of : Instantiate layout elements at runtime
• While showing dynamic data
When your application need to show dynamically some information for example loading title from web then you need as many text-view per title and you are not sure at design time about this at that time this can be preffered
You can make your application design both ways and both will help you the same,It will depened on your requirement whether you require to create it at runtime or not,Anyways I think while you can make separate XML file why need to do it in your Activity java file and get more complexity in code unnecessarily.
XML layout method is very easy, fast and easy to visualize. You can do a paper-pencil work first and simply do it in XML.
XML layouts are easy to manage - you can change the caption, position, look & feel, size, colors etc. in the XML layout, without altering a single line of the Java Code. The App will generate the Java Code for the layout from your XML file.
XML layout does not compell that you should write the whole XML codes. Instead, you can use a GUI editor and you arrange the controls on screen, and it will generate the XML for you. There are lots of GUI design tools. Eg.: http://www.droiddraw.org
Now, you can go for Java Code UI layout only if you cannot determine the layout at the time of writing the App. Say, you are designing an Android App for Web Designers - An app to create HTML Forms and create the HTML code (a simple HTML editor tool), then you are unable to know which type of layout the user is going to do. At this point, the better option is choosing Dynamic Layout (or Java Code layout)
Almost every Android programmer chooses to do layout in XML. That's the beast and easiest way to do it.

Theoretical question: load external XML-Layout file in Android

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.

Categories

Resources