Hello! Hope you’re good and I can help you with some small UI improvements. In this article you’ll learn about merge
, include
and ViewStub
, which will help you to have a better layout structure.
Everyone know, or at least should know, the importance of reusing code instead of copy and paste the same snippet all over the project, but almost nobody do the same with UI code, the XML one. But why?
Well, since you’re here there is a chance that you want to make a better UI code or at least have curiosity for it. So lemme guide your first steps.
The code below will be our base to everything that we’ll do here.
base_layout.xml
Include is the key for code reusability in Android. With include
tag you can embed layout pieces inside your current layout. Everything starts here.
Imagine that we want to add that View
that we called as toolbar in the code above to others layout files and we don’t want to copy and paste it for every file.
The first thing that we can do is to extract that code to another file and it would be like this:
toolbar_layout.xml
Note that I’ve added a layout_height
bigger than the View
height and a blue background
to the ConstraintLayout
.
Now we need to include
our toolbar layout in all files that has a toolbar. Our code and design will looks like this:
#kotlin #code #android-app-development #ui #android