25

Android SDK offers to developers to write layouts in xml files or directly in java code. I have read about it and everyone says that xml are easier to maintain and simplyer to write, but they are static. Otherwise java layout are dynamic, but no one comments performance issues. The only one thing which I found about performance was nesting layout have negative impact of performance. Instead of writing nested layout it's better to make custom one. So far so good ;).

What about layout design? xml or java code? How are xml files parsed my android, at runtime or compile time? Even if they are parsed at compile time, they should be used in runtime. So which will be faster to declare RelativeLayout in xml or to declare it in onCreate method?

Kamen Stoykov
  • 1,715
  • 3
  • 17
  • 31
  • Instantiate View is a bit faster than inflating View from XML file. Unless your Java code is too bad. – nhoxbypass Jan 06 '17 at 09:17
  • Best advantage of using XML over runtime creation of layouts is "YOU CAN SEE THE PREVIEW without having to run it each time" ., And I believe that itself is a strong reason. – eRaisedToX Mar 02 '17 at 11:05

3 Answers3

23

A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. You can declare a layout in two ways:

  • Declare UI elements in XML. Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts.

  • Instantiate layout elements at runtime. Your application can create View and ViewGroup objects (and manipulate their properties) programmatically.

The Android framework gives you the flexibility to use either or both of these methods for declaring and managing your application's UI. For example, you could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time.

The ADT Plugin for Eclipse offers a layout preview of your XML — with the XML file opened, select the Layout tab.

You should also try the Hierarchy Viewer tool, for debugging layouts — it reveals layout property values, draws wireframes with padding/margin indicators, and full rendered views while you debug on the emulator or device.

The layoutopt tool lets you quickly analyze your layouts and hierarchies for inefficiencies or other problems.

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.

When you compile your application, each XML layout file is compiled into a View resource. You should load the layout resource from your application code, in your Activity.onCreate() callback implementation. Do so by calling setContentView(), passing it the reference to your layout resource in the form of:

I think its too much just visit this link http://developer.android.com/guide/topics/ui/declaring-layout.html and get more information..

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
user370305
  • 108,599
  • 23
  • 164
  • 151
  • 1
    I can write answer in my words but I think You get better understanding from Android Developers Documentation. That's why.. :-) – user370305 Dec 14 '12 at 11:53
  • 1
    Thank you for answer, I have read android documentation, but what about the performance? Is view resource loaded faster than one made by code? – Kamen Stoykov Dec 14 '12 at 11:54
  • 2
    @KamenStoykov, As long as you declare your XML Layouts properly, there's no noticeable difference at all. – Vinay S Shenoy Dec 14 '12 at 12:02
  • 2
    Nothing worst on performance.. Both are same.. Actually XML layout is a JAVA class representation.. – user370305 Dec 14 '12 at 12:03
  • Thank you for answers. So all I needed to know is that declaring RelativeLayout in xml or making rl = new RelativeLayout() is absolutely the same. – Kamen Stoykov Dec 14 '12 at 12:12
  • 2
    Actually, when you declare `rl = new RelativeLayout()` in your code it also compile and execute. But you are writing in java format. the same way when you are write `` in XML it also compile as java class code and execute. And in XML you have some representative format.. – user370305 Dec 14 '12 at 12:39
  • One correction to otherwise great post : Hierarchy Viewer tool does not work on devices for security reasons, it works only on emulators – Atul Kaushik Feb 22 '13 at 19:32
4

One primary factor to decide where to declare a UI component is it's behavior during the app life cycle. If it's static I'd declare all attributes needed in XML, if it changes according to the user interaction I'd have to implement it in the activity.

I would go for XML as much as possible for the benefits mentions above like coherence, better management for various screen sizes, localization, resource management etc.,

Karthik Kota
  • 423
  • 1
  • 5
  • 10
1

It is not much of a difference performance wise because your resources xml file will also be precompiled using aapt tools.So the XML conversion will happen to source resources files and alongwith the java source files they will be compiled to Java class files . So it is also logical that performance should be around same only because ultimately both will be compiled and memory for both will be allocated at run time. Also i checked with my sample application that making similar views via XML and via Java and while profiling them i found that the rendering of Views takes around same time. :-)

Rameesh
  • 31
  • 2