Which is the best way to use one xml layout for more than one acitvity?

Viewed 46

Considering 2 activities and the layout of both being very similar with few changes in views, which would be the best practice, and/or which would have the best performance of these cases?

Case 1: one layout.xml for both activities and the minor changes being programatically changed via visibility changes and setTexts(), etc..

Case 2: One base layout with the core views, and one different layout for each activity including the base layout and making the necessary changes.

Considering a simple app but with a lot of different activities.

1 Answers

This depends a little bit on what you expect to change in the future, but generally I would go with case 1 for simplicity. In our production code at enterprise level, we typically go with option 1 because we can make xml changes all in one place.

For a simple example, consider a profile screen. It should be able to be seen as display, but sometimes we want user to be able to edit it. Instead of using two different layouts for basically the same screen, we use the same layout and set the EditText and picture to either be editable or not.

Related