Skip to content
Advertisement

How can I conditionally ‘remove’ or hide a button?

I have a fragment containing a recyclerView.

default UI

When an item is clicked on in the recyclerView I set the recyclerview item background to green and change the “saveBtn” text to “Update”.

after click UI

I need to also be able to remove the “deletebtn” every time a user clicks on a recyclerView item, or hide it so that the UI looks somewhat like this:

UI change on recyclerView click

How could this be done?

Method I am using to update UI on recyclerView click

JavaScript

XML

JavaScript

Advertisement

Answer

Using LinearLayout as a direct child of ConstraintLayout defeats the whole purpose of using ConstraintLayout. It is not a good practice to use nested layout from the performance point of view. Besides, if deleteBtn is direct child of ConstraintLayout, then setting the layout_width of saveBtn to match_constraint will enable it to take up the whole space if we change the visibility of deleteBtn to gone.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement