androidandroid-layoutstacklayout

Android: an easy way to alternate UI elements in one place


There is a dialogue, in one place of which I need to show either one element or another, depending on the situation. Example:

enter image description here enter image description here

I would like to do this so that the elements below do not move. I want to keep the area occupied by alternating elements of a constant size.

What is the easiest way to do this?

I can, of course, manually change the visibility. Вut when switching, if there is a different height, then the underlying elements will jump. I can manually set their height equal, but this is inconvenient. It will be necessary to correct the heights of all alternating elements every time after I change one of them.

For example, Qt has Stack Layout that allows you to alternate elements and takes the size of the largest of them. Does Android have something like this?


Solution

  • You might be able to use the ViewSwitcher to hold the two layouts.

    It holds 2 different child views and measures its height to the biggest child by default.

    Here's the documentation for it: https://developer.android.com/reference/android/widget/ViewSwitcher