• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps UI Code vs XML

The_app_main

Member
Sep 10, 2011
78
1
Hi, I'm new to android development and I was wondering how the more experienced users create their UIs. Do you prefer to write actual java code or use the xml method? The xml method seems easier but not as powerful. Are there advantages / disadvantages to one method over the other?

Thanks
 
Do you have an example where doing the UI only with code is faster than using XML?

I've made my UIs, until now, only with code and the XML seems like a breath of fresh air.

Maybe I'll get sick of using XML too and revert back to code for my UIs but I'll go with XML for now (just started developing Android apps, so I'm new to this field.. I've done desktop applications mostly).
 
Upvote 0
I use code method: usually i create an absolute layout and add views to it.
Advantages:
1)I can calculate all view sizes and coordinates depending screen parameters. So, i hav't support screen problems.
2)Usually XML UI design take a lot of time, code is faster.
3)Really, code design is more simple, IMHO.


1) You can do this with XML too, and it is actually must easier (and more efficient) to do it in XML.
2) XML design is actually much faster than code design(both performance-wise and the amount of time taken to do it) if you know what you are doing.
3) This is just entirely untrue. Using hte XML cleans your code up a lot--you don't have all that UI stuff (which in large applications is a lot) in your code, ultimately making it much more readable.

I should also note that the use of XML is considered standard practice when it comes to Android development and, no matter what platform you are developing for, adhearing to standards is one of the things that seperates good code, from mediocre to bad code.
 
Upvote 0
Look at this app: Engineer calculator - YouTube
How to develop you this with XML? Also it should look alike at all screens (all lines should be 9% of screen height, buttons should spend 100% screen width (in ANY screen)). Yes, it's possible, but it spend *2-3 more time, than code method. Is it efficient? With XML you have more files for different screens. In code method only one file contain IU for one activity: one interface - one file, it is logical, isn't it?
you don't have all that UI stuff (which in large applications is a lot) in your code, ultimately making it much more readable.
You can accommodate IU design code in separate method, which can be minimized.

I should also note that the use of XML is considered standard practice when it comes to Android development and, no matter what platform you are developing for, adhearing to standards is one of the things that seperates good code, from mediocre to bad code.
"Ad populum" argument. It is dogma. XML method SEEMS easier. Fresh developer made some siple apps with XML and it enjoy him. After some developr see, that his apps looks normal only in some screens. So he teach compatibility technologies in XML, because he habit to XML.

Also, code method more versatile: really, you are UI king. In XML you
depend screen configuration, orientation and different things.
 
Upvote 0
You could easily make that design in XML. Anything you can do in code, you can also do in XML.

Also, code method more versatile: really, you are UI king. In XML you
depend screen configuration, orientation and different things.

The whole point of the XML is to make it so you don't need to know the screen configurations. XML offers dp (device independant pixels), and you can create seperate folders, such as layout-land and layout-port, to specifiy the layouts for each orientation. When this is done, the android system automatically handles the swapping of the layouts when the orientation is changed.

You are arguing code is better than XML yet you obviously don't know how to use the XML how it was intended.
 
Upvote 0
You could easily make that design in XML. Anything you can do in code, you can also do in XML.
Arguments, please.

The whole point of the XML is to make it so you don't need to know the screen configurations. XML offers dp (device independant pixels), and you can create seperate folders, such as layout-land and layout-port, to specifiy the layouts for each orientation. When this is done, the android system automatically handles the swapping of the layouts when the orientation is changed.
I know all it. Please, solve here equation for convert dp to screen size (width or height) procents: 1dp=x%. Find x. It should be universal (for any screens). It is inpossible. Only procent design can save you from problems with compatibility.
Are you disagree wit this ( http://androidforums.ru/dp-dip-v-android-t9197.html#entry64800 ) comments? It is russian forum, but it easy translte with Google.
Made a lot of folders -> a lot of files spend more time.
Also, with XML you (de-facto) cannot move you components in code.
 
Upvote 0
1dp = 1px
Untrue, read russian forum stuff.

First of all, UI Design shouldn't be based on percentage of screen. If, for some reason you do need to do this, then it's not hard to use some trial and error.
Some screens have nonstandard sizes. Do you make so many layouts?

That is not true. You can certainly move your components in code if you create the UI in XML.
Really, you can move this components. But i mean de-facto it irrationally, because you should create LayoutParams objects and setParams. With code method you doing this is to create an interface.

I should also note that floating point operations are much less efficient than loading in a few XML files.
This difference is so small that it can be neglected
 
Upvote 0
Untrue, read russian forum stuff.


Some screens have nonstandard sizes. Do you make so many layouts?


Really, you can move this components. But i mean de-facto it irrationally, because you should create LayoutParams objects and setParams. With code method you doing this is to create an interface.


This difference is so small that it can be neglected


1) You didn't read my whole paragraph on that. I know that 1dp != 1px in general.

2) You should make a version of your drawables for each hdpi, mdpi, and ldpi and include them in the appropriate folders. Layout XML files should be placed in either layout-land, or layout-port and then,when referencing a drawable, the android system will determine which one to use.

3) Due to poor English (I'm not insulting you, some people don't speak English natively, and I ma aware of this), I do not understand the third comment

4) Yes, this is true, but you were saying XML files had high overhead. While XML files do have overhead, float calculations are much less efficient, even though both are negligable.

I see that there is no swaying you from your decision to use programmatic UI development, but it should be noted that, unlike Java SE, Google intends Android UI to be done via XML, and standards exist to be adhered to. Although you might see your code has being better, when it goes against standards, it doesn't matter if it truly is better, it will be snared at by other developers.
 
Upvote 0
You didn't read my whole paragraph on that. I know that 1dp != 1px in general.
With XML you can't to develope percent design->can't to develope right design

You should make a version of your drawables for each hdpi, mdpi, and ldpi and include them in the appropriate folders. Layout XML files should be placed in either layout-land, or layout-port and then,when referencing a drawable, the android system will determine which one to use.
No subject. I know all it. I am interesting, do you develope layouts for all screen sizes and densities? Usually design not need pictures.

Yes, this is true, but you were saying XML files had high overhead. While XML files do have overhead, float calculations are much less efficient, even though both are negligable.
At first i mean it comment wrong. For subject: alldevelopers has different efficient of XML and code way. It depending:
1)Design type
2)Developer experience
If design is simple (approximately 5%) XML always more efficient. If design is complicated, fresh developer has significant advantage with code method. Else code wethod is little more efficient.


It will be snared at by other developers
No. Code method is better, it use more expierence developers. For new coders XML very harmful: more developers undestrand object programming worse. Also fresh coders loss more apps because of poor UI (good XML design requires a long learning).
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones