Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance in XML layout vs layout in code?

Is there any performance difference between layouts done in code vs XML layouts in android?

like image 301
nightlytrails Avatar asked Mar 22 '26 11:03

nightlytrails


1 Answers

Yes. XML layouts need to be loaded, parsed and inflated to generate the View. Behind the scenes the LayoutInflater does exactly what you would do when writing layouts through code, but with the overhead mentioned before. Here is an interesting article on this topic, which covers View generation through code, also it is about a library written in Kotlin: https://nethergrim.github.io/performance/2016/04/16/anko.html

But even though there is a performance win, I would not recommend to write layout in code for the following reasons.

  • You couple your layout and your business logic. That's always bad.
  • You can't use the features of the AppCompatDelegate (loading Views for the latest Androind version. E.g. an AppCompatButton instead of a normal Button.
like image 97
rubengees Avatar answered Mar 24 '26 00:03

rubengees