How to show a list of Spinners inside a Spinner in Android [Kotlin]

Viewed 29

I tried to find the answer for this and found something like nested spinner. I understood the concept of nested spinner where we take the values from one spinner and use it inside the child spinner to inflate the values to be shown.

But how can I have something like a Spinner inside that Spinner dropdown itself. Instead of a Textview to be shown I want to show a Spinner and this Spinner will have different values.

Image for reference : enter image description here

As you can see from this image there is a Spinner called as "Choose Profile" and it has 3 values. And all these values are not just simple textviews but another spinners. I would like to understand what is the approach to make something like this.

1 Answers

Well, first of all, I would like to try to dissuade you from doing this. From a user experience perspective, it's horrendous. Spinners opening on top of other spinners will get very confusing very quickly.

And from the programming perspective, it's a pain as well. You would need to create a custom adapter for your spinner, where every view has a spinner in it. And that means that you would have to track nested spinners returning their values. The code will become very confusing, and maintaining it later will be even worse.

Having said all that, if you want to do it anyway, you'll need to create a custom Adapter for your main spinner. Inside each custom view (or "row"), you would create a spinner as well, so every "row" would have another spinner in it. You can see how to create custom adapters here for the general process of doing it.

Edit: A better UI approach would probably be to use just a dialog with a ListView in it. You can still use a Spinner for the initial menu, but when the user chooses one of the options, it'll pop up a list of options for them to pick. Since it's a Dialog, it'll be centered on the4 screen, with the rest of the UI behind a scrim

Related