0

I´ve been working on a project, where the user should be able to choose an option from a spinner and depending on the selection, a specific form should be generated.

Now, I just have one standard android-xml for all the selections, and an onItemSelectedListener for the spinner.

I´m not sure how to get this going. Should I generate all the layout in plain java, or should I make a xml-layout to include in some way?

1 Answer 1

2

If I understand it well you want to create a short of configurable form.

To do so you can create a XML layout with all the available options of the form. Then by code, depending on the user selection add or quit elements with the View setVisibility() method and show the layout.

In case you have many forms or they are too big or complex, you can have as many XML layouts as different forms you have and then show one or another based on the user selection.

Sign up to request clarification or add additional context in comments.

2 Comments

The setVisibility() method could work for now. But I have a feeling that the layouts will be more complex in the future, and I know how show them depending on the selection, but will I then have to copy all the methods connected to the views to a new activity for each XML? An example: I have a picture of a car, and a Spinner selects the type car. I want the picture and the spinner to stay there, and below these two components generate a form, changing depending on the spinner-selection.
I thing it depends on how different are the forms one from the other, or how complex they are. If they differ a lot, then I would create two different layouts and two different classes to handle the logic. If they are almost the same but only small functionality differs, then you can just add all the logic in the same file. If they differ in the kind of logic you can apply subclass. ie.:lets say you have a form for car registration, the form will vary depending on the kind of car, vagon, truck, motorbike, but all of them will request owner name, license plate, etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.