I\'ve got a view that is effectively is a button. Here is its XML layout (add_new.xml)
All replies in this thread are suggesting a new wrapper view, which comes at a cost. The correct way of hiding a view completely is to set margins to 0 while setting visibility to GONE. In this code sample, cardView is the view I am trying to hide. The direct parent of cardView is RecyclerView, that's why we are using RecyclerView.LayoutParams - remember to replace with the right layout params.
if (cardView.getVisibility() != GONE) {
cardView.setVisibility(GONE);
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) cardView.getLayoutParams();
layoutParams.setMargins(0, 0, 0, 0);
cardView.setLayoutParams(layoutParams);
}