Archie.bpgc
Archie.bpgc

Reputation: 24012

Android: Replacing the Views of the Layout dynamically

In my application i want user to fill in a large form (5-10 fields). Not all the fields are EditTexts, some are Spinners, RadioButtons etc.,

So instead of have them all in a single layout(user may leave the screen, seeing so many questions). I thought of having it as a Conversational/ questioner like thing.

On submitting each answer eventually shows a new question.

Instead of having separate Activity for each question, better i change the ContentView of the same Activity.

Solutions like:

1. Make View's Visibility Visible/Gone depending on the question to be asked

2. Change the ContentView and all onCreate().

are inefficient when i have to change the ContentViews or Visibilities 10 times.

Should i use a Fragment in the Layout and replace it with new Fragment?

Even that way i end up having 10 Fragment classes and 10 layouts 1 each for th Fragment Classes.

Is there any other way to implement this. Right now i am doing it using Fragments.

Thank You

Upvotes: 0

Views: 388

Answers (2)

Gan
Gan

Reputation: 1399

I had a similar situation where the user had to fill in a few requests, there was a status and navigation pane which remained constant throughout the app, so it didn't make sense to start a new activity every time. So I made a main layout with a three Linear layout utilising the top and bottom for navigation/status and the middle was used to display the questions.

I created the different layouts and inflated/deflated them as required since i had only a few (3) layouts. If the number of layouts increases then the method to keep track and inflate/deflate them might become complex.

Adding and removing layouts/questions is rather easy, if you have a sequential flow. One downside to this approach is the backstack, you have to implement the logic yourself as you stay within one activity and a backpress will exit the activity. This approach also has some advantages since all the data you collected in the questionare is readily available and is a boon if your next questions are based on the previous answers, it reduces a considerable amount of work in having to pass them along in intents.

However, I do not have much experience with Fragments since I opted for this approach since it was much simpler than fragments and required less time to implement for my requirements.

Upvotes: 1

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

I believe, Fragments would be the best choice here, since either ways (i.e. either dynamicaly or creating layout files) you have to write it 10 times.

So better would be Fragments, since, each layout will get replaced with the previous layout dynamically.

But you also have to consider one situation here,that is, if in future, you have to add new questions in your form, then you will have to do a lot of modifications in the code, otherwise, this is the best place to use Fragments.

Upvotes: 1

Related Questions