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

101 views Asked by At

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?

2

There are 2 answers

0
SalicBlu3 On BEST ANSWER

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

0
Thomas On

Just an idea if you can't find something like Stack Layout. I haven't tried it.

You can put all the elements in an horizontal LinearLayout with MATCH_PARENT width for the visible one and 0 for the invisible ones, but keeping all of them VISIBLE. It should always have the largest height and only the MATCH_PARENT width element should actually be visible.