I'm searching for a way to 'inflate' an Android XML layout which wasn't known while compiling. I already searched for hours and always found the answer, that this wasn't possible, because the LayoutInflater can't use simple XML files. Ok, but unfortunately, this is what my app needs to do.
My app receives an Android confirm XML layout, which it has to display. No interactive functionality is needed, just graphical display.
My first idea was to deserialize the XML file manualy and build the content programaticaly. I'm sure this will work, but is there a simpler way or even a library, I could use?
EDIT
As I was asked to post some code, I think I have to add some information. I haven't written a single line of code for this project so far. I wrote much code to read third party XML layout definitions (which weren't Android) for a prior project and 'convert' them to Android Views on runtime. Of course, I could do this again, but this was a lot repetitive work. So I'm looking for different approaches.
There's really not a simple way to do it. You could use Simple XML http://simple.sourceforge.net/, this will ease the pain of parsing and you can use a generic approach like binding some kind of "pattern" of your generated xml files from the server to an element. You can also get some inspiration from this tutorial http://www.ibm.com/developerworks/xml/tutorials/x-andddyntut/.
I hope this helps.
Related
After developing few applications, I know that many people like to add view programmatic ally.
But is it true that all XML layouts can be converted to Java code dynamically?
What are the pros and cons of using Java code to generate the layout?
The layout XML you are creating is being translated at run-time to java code.
So the answer to your question is Yes. You can do the same and basically create all your layout in java code from scratch.
The obvious down grade of this thehnic as you know is that it will take you much more time to achieve the same result as with the XML file.
So, unless you have to add view dynamically to you layout, I don't see a reason for you doing that process using java code.
But is it all xml layout can convert to java code dynamically?
Yes
What is the pros and cons of using java code to generate the layout?
According to the documentation the advantage to declaring your UI in XML is that it enables you to better separate the presentation of your application from the code that controls its behavior. Your UI descriptions are external to your application code, which means that you can modify or adapt it 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. Additionally, declaring the layout in XML makes it easier to visualize the structure of your UI, so it's easier to debug problems.
From Hello Android by Ed Burnette:
User interfaces can be designed using one of two methods: procedural
and declarative. Procedural simply means in code. For example, when
you’re programming a Swing application, you write Java code to create
and manipulate all the user interface objects such as JFrame and
JButton. Thus, Swing is procedural.
Declarative design, on the other hand, does not involve any code. When
you’re designing a simple web page, you use HTML, a markup language
similar to XML that describes what you want to see on the page, not
how you want to do it. HTML is declarative.
Android tries to straddle the gap between the procedural and declarative
worlds by letting you create user interfaces in either style. You
can stay almost entirely in Java code, or you can stay almost entirely
in XML descriptors. If you look up the documentation for any Android
user interface component, you’ll see both the Java APIs and the corresponding
declarative XML attributes that do the same thing.
Which should you use? Either way is valid, but Google’s advice is to use
declarative XML as much as possible. The XML code is often shorter
and easier to understand than the corresponding Java code, and it’s
less likely to change in future versions.
Pretty much everything that you can do in the XML can be done programmatically. I think it gets very difficult when it comes to drawable shapes and such.
I for one only do layout stuff programmatically when I don't know beforehand what I need to add.
Example: I have some Articles that can have 1- x descriptions, I don't know. Then I programmatically create TextViews that I add to a layout.
This question has been bugging me for some time. I've already developed a couple of apps on the Android platform and somehow always find myself resorting to Java code in order to construct the layouts. In a professional development environment, is this acceptable? Or should XML files be the go-to approach? I usually find XML a more tedious approach and often, these layouts don't look the same on all devices. I just don't get it. From a professional viewpoint, has anyone really been able to develop apps with complex views purely using XML files? This question is killing me because Google recommends using XML but the UI never looks the same on all devices unless done programmatically. Or am I doing something wrong?
Note that I'm referring to Android 2.2 and 2.3, which majority of the users use.
I use XML layouts on pretty much every fragment and activity of every app I write. I very rarely see any need to create Views dynamically, tho configuration of ListViews, showing/hiding views, etc needs doing in code. For me the advantages of XML are:
Ability to use layout editors (Eclipse)
Easier to preview layouts
Possible to benefit from auto-localisation of layouts
Easily maintain different parallel layouts for difference devices (screens)
Can get a sense of the layout by looking at it (easier than code)
Easy to break layouts down into pieces (fragments, includes, etc) to remove duplication
Keeps a separation between the visual design, and the functionality behind it
I can't think of any good reasons to put all my layouts into code - that sounds like hell.
I expect the reason your layouts don't look the same is because your XML is not defining the layouts correctly. Bear in mind the Android tools convert XML layouts into code, so there's no inherent problem with using XML layouts versus dynamic - both end up as code.
OckhamsRazor,
The answer very much depends on your needs, flexibility, and knowledge. The first thing to understand is that every Layout, whether created via XML or programmatically can be tweaked specifically or made to conform to many screens via properties.
... and somehow always find myself resorting to Java code in order to construct the layouts. In a professional development environment, is this acceptable?
Yes, it is. Android makes those available so you can do just that. However, the benefits of managing layouts via XML include standard MVC segregation, simpler debugging, and an easier time modifying the resource, if needed. Additionally, you may maintain multiple copies of Layouts depending on device configuration easily.
... has anyone really been able to develop apps with complex views purely using XML files?
Absolutely! There are some amazing programs that fully utilize XML rather than programmatic views. The key to them is how much information (that is non-standard view properties) is required from parental Views. Even in those cases there are ways to pass that information provided you know where and how to do so.
Or am I doing something wrong?
I don't think so. Honestly, I've run both ways depending on need. I'd say it really comes down to your lack of knowledge of the quirks. But the job is to get the job done. Here's an example: There are some times when I don't know how big everything needs to be until its run on the device, and there are times that I make the device conform to my layout's needs. Ultimately, I use the following chart to make my determinations.
Do I need information from parental Layouts that is aside from view properties
Do I need to dynamically size more than one element independently.
Is the View type pre-determined or will it change as well?
If the answer to 2 out of 3 of those is "yes", I will use some level of programmatic layout. If not, I will go pure XML. That being said, programming is one of those professions that encourages ingenuity (provided it is safe) and nearly anything can be accomplished in any number of ways. Ultimately, I'd say do whatever makes your job making quality apps easier.
Google makes its recommendations based on their own knowledge of software programmers and their general practices. They also created the platform, so they know which things are optimized in which ways. Its all about experience and we all have our own. If you have trouble utilizing XML, its worth taking the time to figure out the quirks simply so that it is another tool to utilize. Also, it will give you the information you need to answer this question for yourself.
To sum things up: I could say chocolate is better, but if you like vanilla, you'll disagree. Be aware of the drawbacks and benefits of each and take the time to learn how to accomplish the same tasks with both methods. It will make you a better programmer and give you a better sense of when to use which technique.
Hope this helps,
FuzzicalLogic
I typically do a lot of work with highly customizable UIs, where large portions of it need to be done in code. That being said, wherever possible I try to use layout fragments and inflate them, so as UI sections are added, removed, or rearranged I'm still just doing some of the layout, not all of it.
That being said, it's not that hard doing layout via code. The big advantage to it is compile-time checking. I'll find issues that way faster than using the preview pane. The preview pane can be nice for initial layout, but I use the Hierarchy Viewer for figuring out why my layouts don't look right.
It really depends on what type of project it is, or piece of a project, and what type of programmer you are. Some people just prefer pure code, while others like leaning as much on other tools for design as possible.
XML definitely has some benefits, like being able to switch between interface designs quickly. For specific design themes that are repetitive, is definitely useful for most programmers.
I personally prefer doing everything programmatically, and it is quicker for me to develop than writing XML, with the libraries and classes I have created. XML is quicker straight out of the box.
As for performance, there really isn't a difference worth mentioning unless you are using the same view so repetitively, at the same time, to the point that it no longer fits on the screen many fold. I did a test of how many text views Android could render on a Moto X - Android 4.4, and it couldn't get much over 5000, but there is never a purpose for that. If you are at that point, you are either need to dynamically load and unload data or are just doing something very wrong to begin with.
So learn both sides of it, definitely get to know the pros and cons with your style of programming, because there is no right answer for everyone, and let loose and have fun.
It is much better to separate the layout and put it in the xml file. I occasionally have to adjust the layout in code, but it is always an exception and only when I determine that it cannot be done in the layout .xml. If you use the layout views correctly, the application should look very similar on all devices.
I want to be able to generate HTML representations of existing Android layouts. In my case it's to help document some of my applications for users who are blind, otherwise simple screenshots would probably suffice.
Since layouts in Android use XML I expect we can convert them relatively easily. I can write the code to do so for the layouts I use, however I would prefer to use something that already exists, or at least get some pointers on how to map the XML to equivalent HTML would be great. I'm considering using DroidDraw as a possible base for the work if I do it myself http://www.droiddraw.org/
XSLT is easy way to translate one XML into another, however there is a lot of work with defining html equivalents of android widgets.
You can find examples here
How to run XSLT procesor you can find here
XSLT and XSLT processors are common tools, so you will find lot of code and examples. This is a bit too complicated to just put here full solution ;)
I am very new to Android development and, while I get the general premise (and have even built a small application), I have been looking at other developer's source code to get an idea of how to better approach my development for larger projects.
One developer's code is read is basically using both XML layouts and Views for the various parts to the UI (similar to what is being asked in this question). I understand what he is doing, but it seems overly complicated to me. The XML layouts provide functionality already to create responses to actions. (For example, "onClick" is provided for most components in the XML.) Layouts can be generated very easily with the XML.
So, my question is - can I get away with building my entire application using just Activities and XML layouts? Can I choose not to use any Views? (Of course, this is assuming a relatively simple app - think task list or something similar.) Or, am I trying to simplify too much?
The general strategy I use is to push as much as possible into XML. It's a very different way of thinking from some other UI development systems, but it's very cool once you get past the learning curve.
I don't know what you mean by choosing "not to use any Views". Every UI component is a View of some sort. If you mean not using any custom View subclasses, then yes, it is definitely possible. The only reason to create your own custom View classes (and then use them in XML!) is when the stock widgets and views don't do what you want. Since they are quite flexible, this tends to be fairly uncommon (until you start getting into fancy behavior or need custom graphics behavior).
There are two ways for Creating UI for Android Application. They are
Using XML - You can use xml for designing UI targeted for supporting Multiple device. Also XML helps you to create Static components.
Java Code -Generally it's not a good practice to creating UI in java. Its suitable, if you creating a samll application. Its also useful when you want to develop application with dynamic components. If you want to create Dynamic Components in UI, Java code helps you to achieve this.
The Good Approach is to create UI via XML, unless there's no dynamic component needed in the UI. if you need dynamic UI creation then you go custom UI creation i,e., Using Java Code.
Since you are New to Android, i would like you to refer android developer site
I think you misunderstand, XML layouts are just a shortcut for creating views. You end up with the same results at runtime either way. Mix, match, use one or the other, it's up to you.
I've been through the Android tutorials - these do a good job of introducing how we can hand-roll an Android user-interface. Actually, I do not need that level of control right now... I'm looking for something simpler...
I'd like to make an Android app which will mainly contain a number of standard UI widgets, nothing particularly fancy. Having done some VB development a long time ago (yes, I know it's crap!) - I particularly like the ability to paint user-interfaces with an interface designer and then add in the relevant callbacks via the IDE. I'm using Eclipse, so for now solutions requiring net-beans or other IDEs are not particularly helpful.
I'm well aware that this practice often produces sub-optimal code, and less than beautiful interfaces. That's not really a concern here. I just need to produce a certain effect quickly in order to prove a concept. There will be plenty of time later on for optimization if my idea is good enough.
If you create a layout xml file you get "drag/drop" for the activity layout. It's not perfect, but you should be able to accomplish what you're asking for.
How you were used to VB development won't work out for you.
You will have to create your interface in XML, and put events to the objects by code. There is DroidDraw but it won't get you further then the plain inbuilt IDE of Eclipse.
When creating XML layouts think like it a HTML layout, nested objects, tables/linearlayouts etc...