Performance: Android views generated programmatically vs xml views

Viewed 681

Suppose I want to inflate 50 views inside my fragment/activity/app programmatically.

Compared to xml, does generating views programmatically in Android affect the performance of that particular fragment/activity/app?

If no then would there be any visible effect? If yes then can you please explain why?

1 Answers

The performance is not affected, but each has its pros and cons. Just to name a few:

XML

Pros

  1. The UI separated from code
  2. You can reuse layouts, combine them
  3. Faster development, you can check the UI in preview, without rebuilding the app

Cons

  1. Its static

Programatically

Pros

  1. React to runtime conditions

Cons

  1. more code (e.g. harder to maintain your code, more potential bugs)
Related