Programmatically or in XML layout

Asked

Viewed 85 times

1

I made a layout in XML, with time I began to realize that the layout programmatically has more facility to model the screen as the user interaction. Is it programmatically better than XML? And if it is, there is some "tutorial" creation?

  • Before anything, start by doing the tour to understand how the community works; then read the How to ask to check how you can improve your question and finally access help center to check various articles that will help you better understand the site.

  • Sorry, it was the rush and frustration to send the question. I will be more careful in future doubts....

2 answers

2


In general it is like, but you can say that the imperative code (what you are calling programmatically) will always be more flexible or powerful, unless the technology has a lot of "magic", and even then it will only be better in some specific points where the magic was created. The declarative form (which AP calls XML) may be more practical at certain points because it eliminates certain Boilerplate and adds some, and allows a form that designers are more accustomed, especially if you have used HTML. The declarative form, although used to give information to the application to program the computer device, is not exactly programming. A declarative form can never have the power of programming.

If you want to do layouts dynamics so the solution is almost always to do programmatically. It has some ways to give certain dynamism declaratively, but it is very limited.

I don’t know the stack Android technology in depth, but the declarative form tends to be slower, especially if this is interpreted at runtime. Even if a code is generated at compile time from the declaration made in XML, it should still be slower. It is like comparing a high level language with Assembly (well done), it is very difficult for the high level language to produce an Assembly as optimized as you can do at hand. The point is to do it well, to make the code in the hand can be worse if it is done badly, but it is more rare to make this kind of mistake, the person has to be very clueless, unlike what happens with Assembly that has another level of abstraction.

This cost of performance can even be intermediate, that is, generate a programming language code in the process of Jitting.

I also do not know if there is any disadvantage to do in code, has technology that uses a form of XML that has a code to distribute much smaller.

In general people use the declarative form also because it has more documentation and more support from the community, after all if everyone does so can get more help.

I prefer to do it with code, but I don’t rule out doing something declarative if you show me advantages or are necessary for some reason beyond my control. It has no better, taste and need. If the person does not know how to program with imperative code can be worse. So if you’re having any difficulty with accurate analysis, there’s no automatic way to solve this. Developing software is difficult and needs to master everything to achieve good results.

  • I get it, thank you very much, what I’m doing, even if I don’t understand it directly, the imperative code gets more interesting. I will study more about, helped me a lot your information... Thank you

2

What is best in programming depends on the context. The same approach may be better in one situation and not in another.

The use of xml is in most situations, for being declarative, more efficient from the perspective of the programmer. You say what you want and Android is in charge of "how to do it".

In Java it is possible to do everything that is done by xml but it is more "laborious" and needs a good knowledge of how "things" work, since you have to be the one to say how it’s done. However, it’s more versatile.
One of the reasons to use it will be the one that refers: dynamic layouts, depending on user inputs.

An api is currently under development(Jetpack Compose) which allows you to declare layouts directly in the code, in this case using Kotlin.

  • Thank you, I want to do many dynamic interactions, and I found it actually more interesting in imperative code. I will study more about...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.