How to Hide Include Layouts in Android Programmatically with Java

I don’t know about you, but I love modularity. In fact, the main reason why I prefer Android over iOS more (same with Windows) is the ability to customize.

The device doesn’t feel like “my device” until I change it to my liking. That extra bit of customization option does just that. Do you know what I mean? It’s that one little thing that makes it unique, that makes it work best for us, that makes it truly “my device.”

In the case of Android app development, what if we want to give our user the ability to customize the app’s layout based on certain conditions? Or maybe if we want to give our user the ability to customize certain parts of the layout, such as letting them hide certain components or display certain components?

After all, it is not possible to a certain extent to create a “one size fits all” scenario. There is only a provide the user an X amount of options to choose from, and let them decide for themselves while we provide a standard recommended layout or design for them to start off with.

Here, you will learn not just how to hide include layouts in Android, but to also make them reappear.

How to Hide Include Layout Components

So to keep things simple, let’s deal with a common component that we can all relate to, and that is the Android Toolbar. In the beginning, most of us probably started out as a beginner not knowing about modularity in programming when dealing with layouts.

You probably did something along the lines of typing and retyping the toolbar code for each activity or fragment it appears in for your app’s design. Then as we became more knowledgeable and efficient, we learn that we can make components to make things more modular. Not only that, to not reinvent the wheel (DRY – don’t repeat yourself – model). Thus, the toolbar.xml was created.

Let’s say you have a special circumstance, where you have 2 toolbar.xml files (toolbarA.xml and toolbarB.xml), each with a very different layout depending on the user’s settings. A should be activated when user wants A to be the toolbar of choice, and vice versa.

To hide the toolbar in java, you need to use the setVisibility() method. In our specific scenario, you have two parameters to choose from to pass into the function: View.INVISIBLE and View.GONE.

NOTE – This method is used with a View object. So for example, let’s say you created a variable called View myView;. You would do myView.setVisibility(…).

What is the difference between View.INVISIBLE and View.GONE?

If you want to make the component to be invisible, but keep the component still existing in the activity or fragment, you would pass in a View.INVISIBLE parameter, which gives you setVisibility(View.INVISIBLE). You can still reference the component still.

If you want to make the component to not just be invisible, but rather be removed, then you would pass in a View.GONE parameter, which gives you setVisibility(View.GONE). You can no longer reference this component.

Still kind of confused? In other words, using View.INVISIBLE is like Harry Potter using his invisibility cloak. Harry Potter is still there, but people can’t see him. However, when you use View.GONE, you basically destroyed the existence of the component altogether.

Another way to think of this is, let’s say using a trash can example with a trash pickup truck.

When you do View.INVISIBLE, you are basically putting trash into a trash bag, and sealing it up while it is in your trash can. You can no longer see the trash, but it is still there and you can still access it if you realize you accidentally threw something away that wasn’t supposed to be.

When you do View.GONE, the trash pickup truck took the trash from your trash can, and stored it in the landfill. From your view, the trash is gone – no longer in your trash can and you can no longer access it.

How to Make Hidden Components Reappear?

Okay, so now you want to make things show up again. You use the same method: setVisibility(). This time, you would pass in the parameter View.VISIBLE for any of the two cases: View.INVISIBLE and View.GONE.

Final Thoughts

Hopefully after reading this post, you now know how to hide include layouts in android programmatically using Java. If you guys find this to be helpful or have any other useful suggestions, feel free to leave it in the comments below. Or better yet, maybe you could share what you used this neat technique on for your app to make it more customizable.