14

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,

LuckyMe
  • 3,820
  • 2
  • 27
  • 35
  • 1
    Typically you would predefine (and inflate during runtime) layouts wherever possible. This has the advantadge of separating the UI from application logic. Moreover, you can use the graphical layout view without running the emulator (with exceptions, such as unpopulated `ListView`s and the like). – Tadas S Apr 25 '13 at 08:05
  • 1
    I understand what you are saying, but what I want to know is, let us ignore convenience of coding and assume that we completely separate the UI from the application logic in the code, and the code is understandable and clean, now which is faster and better, inflating or coding the layout? – LuckyMe Apr 25 '13 at 08:13
  • 4
    In Official Android Source code, layouts are defined in XML and inflated during runtime. IMO, if you construct layout programmatically, it's not clean. You also lose the ability to preview your UI that you get on the XML layout viewer. – dannyroa Apr 25 '13 at 08:17
  • 1
    @LuckyMe inflating views on runtime is useful when the user needs to add views on user action such as click event or scroll event, i mean when the activity is not going to change on user action. If any user action leads to change of the activity then you should create your views in xml only. This is a better practise i've been implementing. – Chintan Soni Apr 25 '13 at 08:21
  • 4
    inflate is easier, faster, more maintenable, more readable, reusable, a better separation of view/controller. Layout in code is /never/ readable, mostly because it obliterates the visibility of the hierarchy and also because it adds tons of overhead code. – njzk2 Apr 25 '13 at 08:21
  • I see, so inflating is the Google/Android recommended and conventional way of doing layouts, and it doesn't slow down the runtime of the application by much. 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. – LuckyMe Apr 25 '13 at 08:57

1 Answers1

38

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thank you for the elaborated response, just one question regarding _when compared to doing the same work yourself in Java_ you mean to say that the execution of the coded layout would be slower than the inflation, not speaking about the code writing time, rather the code execution time. Many thanks. – LuckyMe Apr 25 '13 at 23:41
  • 1
    @LuckyMe: All of my time references are with respect to execution time. – CommonsWare Apr 26 '13 at 00:05
  • 4
    I've just had a problem with some slow screens - I was adding some semi complex views into a scrollable programmaticaly. Converted it to inflating a layout and using findviewbyid to alter the text values and it is noticeably quicker in this case. Just thought that I would add that for anyone else wondering the same question – CurlyPaul Aug 08 '13 at 11:35
  • @CurlyPaul: Thanks for the input; I was just about to try the opposite (go from inflating to programmatically creating layout) in order to improve execution speed. – SMBiggs Jan 18 '15 at 15:48
  • A fantastic and very helpful answer as always. Thanks. – Alex Mar 06 '16 at 15:20