UI Code vs XML


Last Updated:

  1. The_app_main

    The_app_main Well-Known Member

    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

    Advertisement
  2. dimiter

    dimiter Active Member

    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.
  3. anchinsha

    anchinsha New Member

    has anybody got any experience of using xml editors to work with your xml files, are they any good for ui development?
  4. raduq

    raduq Member

    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).
  5. jonbonazza

    jonbonazza Well-Known Member


    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.
  6. dimiter

    dimiter Active Member

    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 can accommodate IU design code in separate method, which can be minimized.

    "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.
  7. jonbonazza

    jonbonazza Well-Known Member

    You could easily make that design in XML. Anything you can do in code, you can also do in XML.

    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.
  8. dimiter

    dimiter Active Member

    Arguments, please.

    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.
  9. jonbonazza

    jonbonazza Well-Known Member

    Whoops double post. :x
  10. jonbonazza

    jonbonazza Well-Known Member

  11. dimiter

    dimiter Active Member

    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
  12. jonbonazza

    jonbonazza Well-Known Member


    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.
  13. dimiter

    dimiter Active Member

    With XML you can't to develope percent design->can't to develope right design

    No subject. I know all it. I am interesting, do you develope layouts for all screen sizes and densities? Usually design not need pictures.

    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.


    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).
  14. anchinsha

    anchinsha New Member

    take a look at liquid xml editor as well, ive been working with it for a few weeks now, its a much easier way of working with xml.

Share This Page