I am a junior developer and I am somewhat puzzled about writing xml layout files.
There are many implementations for the UI layout of the same style, RelativeLayout, LinearLayout, RelativeLayout And Linearlayout, and so on. Different implementations have different nesting levels. We know that there are as few nesting levels as possible, and that LinearLayout is used instead of RelativeLayout for the same number of layers.
But careful analysis, how do we measure the A implementation of a layout is better than B, two layers of nested LinearLayout really do not have a RelativeLayout to save performance? How to determine this? By experience? But where does the experience come from? It may be a good way to read the source code, but it is more difficult.
I think it is possible to find a good way of writing by analyzing the rendering time of an xml layout file and comparing the time spent on different implementations.
So I would like to ask everyone, is this a feasible way?
I'm not sure about, how we check the rendering time. But check below URL's one after one it may help you to understand more about rendering and performance related.
1) https://medium.com/#elifbon/android-application-performance-step-1-rendering-ba820653ad3
2) http://adavis.info/2015/03/android-overdraw-what-is-it-and-why-should-you-care.html
If i find any way to check the rendering time surely will let you know.
Related
I've been working for a while on a rather complex Android application, which involves a lot of layouts and almost all of them are quite complex. However, I am taking a very long time since it is so annoying to position the elements of each layout, since they're not positioned like the dialog editor I'm used to in Visual Studio:
Everything in Android is literally relative to other elements and moving one element usually moves a lot of other elements, plus I can't position anything properly unless I guess the position more or less and move the element several times by guessing the margin on a certain direction.
So my question is, is there a more effective way to design UIs quickly and professionally, so that I don't have to design in such a sluggish way (as I believe it is)? I already tried several editors but they all seem to be very similar to Eclipse's. Is there a more effective way at all?
Also a side note, I'm using a RelativeLayout on each of my layouts
Do not compare visual studio with IDE of android. It has some limitations.Try to use android studio which will help you more in UI design. We can do UI perfectly in android. Relative layout is the best to position the views but should use wisely. Atleast please ellaborate what type of design you want to implement. Atleast provide a sample Image of the design which you want to create. Then only I can tell what you need to do.
Regarding Android programming,
This question has been bothering me for a while, should I inflate layout as much as possible or should I programmatically create the layouts as much as possible?
If we put "convenience" aside which is better? faster? safer?
Any concrete inputs are greatly appreciated.
Regards,
I see, so inflating is the Google/Android recommended and conventional way of doing layouts
Yes.
and it doesn't slow down the runtime of the application by much
It doesn't slow down the runtime of the application by much when compared to doing the same work yourself in Java.
Because I read somewhere on the developer site of Android that it is a slow process and views should be "reused" as much as possible before resorting to inflating.
Recycling views in an AdapterView -- the most likely place for you to have read that advice -- is important, because recycling views is faster than creating views, no matter how those views are created. Again, the comparison is not between inflation versus rolling your own Java code, but between inflation and not creating new views.
should I inflate layout as much as possible or should I programmatically create the layouts as much as possible?
Inflate, please.
which is better?
Inflation takes into account resource sets (e.g., different layouts based on screen size and orientation). Inflation takes into account overrides of styles. Inflation works better with tooling.
faster?
There are probably some scenarios where hard-coded Java is faster than inflation, just as there are some scenarios in which hard-coded assembly language is faster than C. In most cases, the speed difference is not important enough to warrant the hassle.
safer?
I do not know what you consider "safe" to mean in this context.
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've seen that some Android devs promote this form of declarative layout:
<TextView style="#style/my_textview_style" />
i.e. that pretty much all the attributes (including the ID) go into the style definition. It certainly makes for shorter layout files, but is it a step too far?
What would you recommend goes in the style definition, and what should stay in the layout, and why?
I've started doing Android dev this year, but have prior experience of development. Issues like this are so important, the "way" we use the tools, the habits, to be more efficient. I appreciate you sharing your views on what works for you, for clarity, for efficiency, for maintainability, for app reliability.
I've pondered this very question and it becomes much more pertinent when dealing with multiple device sizes and densities.
Putting too much information into the styles can limit their use, especially when dealing with landscape and portrait layouts. For example a landscape layout may in fact be wholly different in composition but require very similar styles.
In these cases it helps to use the . notation to ammend style differences, for example.
<MyPage>
<MyPage.portrait>
<MyPage.landscape>
See below for information on the above approach to creating styles.
http://developer.android.com/guide/topics/ui/themes.html
MyPage defines the common properties for your layout and you then override these values in the portrait and landscape extensions.
I..e you might have more right and left padding in landscape than portrait.
The problem i've found is that the themes and styles xml files get huge, especially if like me you have many activities, and if you address lots of creative changes then you can end up with lots of messed up definitions.
At this point you have to take stock, tidy up and re-factor.
My only advice is that you plan really well upfront. Define a naming system that accommodates your project, write it down and stick to it.
Possible style naming convention
<section>_<componentName>_<componentType>_<orientation>
home_leftPanel_btn_horizontal
home_leftPanel_listview
home_leftPanel_listview_horizontal
A similar approach to themes should be adopted.
I'm still working this out, so can't give fuller examples.
Pushing those things out into the style is a good practice if you plan on skinning your application differently. If you plan on releasing two versions with just a different look then by all means push those into a style. As you say it cleans up your layout files quite a bit.
Should you push IDs into styles. No because IDs are the hooks by which you're code will grab references to those objects. I can't see much of a point to always having your program externalize IDs. So I'd say that practice is going too far.
Now should you do this every time? Well I think that really depends on if you think you need the benefit this brings to reskin the application. Some people can't say no to a good idea, and try and do every good idea all the time. While that's admirable it's not productive. Experience is knowing what good ideas you need and what good ideas are just fluff for this application your are working on right now. To keep ourselves productive we need only the good ideas that make us move faster, and all other ideas are just art. How do I choose? Refactor, refactor, refactor. Do it fast, then refactor to clean it up based on the next requirements I'm working on. Do it fast, then refactor. Repeat until my boss smiles.
Can someone point me to an Android example with source that allows the user to scroll through the contents of an xml file that contains text entries (preferrably with arrows on the side of the screen that allow previous/next type of interaction)
I've looked through the examples on the Android site and didn't see anything similar. Any help would be GREATLY appreciated (I'm an Android newbie but experienced Java developer)
I seriously doubt you are going to find a nice Android walkthrough of exactly the problem you are trying to solve, so you'll probably need to extrapolate from a few different guides.
Parsing xml is not substantially different from parsing xml anywhere else. This guide talks through the 3 different ways of doing so. Depending on how large your xml is, you can decide which makes the most sense for you.
In terms of UI, not sure exactly what you are looking for, but a ListView is often a good way to display large chunks of data. See Hello ListView.
Or, if you want a single text area that scrolls with 2 buttons, see ScrollView api. Its pretty simple. Common Layout Objects is a good guide to basic layout. You could probably accomplish what you want with a few nested LinearLayouts, or a RelativeLayout. If you post an attempt, people can help you fix it up.