Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: What is the best way to avoid code duplication when several screens of the app have similar layout?

My application has several screens, and each of them has almost the same layout.

I know I can use the "include" tag in xml to avoid rewriting the same layout again, but I also do not want to duplicate the java code of setting the behavior/properties of views and layouts. What is the best practice to be more organized in this case?

Edit: For example in several screens, the first half of the layout is a gallery that scrolls horizontally with text below it. This is the same throughout my app

like image 961
Harsha Vardhan Avatar asked Jan 23 '26 12:01

Harsha Vardhan


1 Answers

If the screens are the same for example. Activity/Fragment

You can create a "generic" parent that handles the logic and use include for the layout.

Or

Create a custom view that uses a layout as content and you can write your logic there.

I create a library that uses this principle. here is a tutorial on how to create a view with a layout file

like image 86
Gil Goldzweig Avatar answered Jan 26 '26 01:01

Gil Goldzweig