0

I would like to create a form in android in a fragment in which a user can add and remove form inputs The below is an image which describes what i mean enter image description here

As you can see there is an option to add a new row or even remove

Could somebody guide me on the way i would implement this, if possible with code snippets or external reference. I have researched and the samples provided arent clear

The provided resource is not clear because, take an instance where you on a button click you are to add a new edit text and set id to 1, next instance the id is supposed to be set to 2......without a limit to the user how do i implement such

Geoff
  • 6,277
  • 23
  • 87
  • 197
  • Possible duplicate of [Android - Dynamically Add Views into View](http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) – Shaishav Sep 22 '16 at 16:30
  • No its not a duplicate since the one you have provided doesnt explain on removing inputs – Geoff Sep 22 '16 at 16:32
  • Alright, thats why it was a possible duplicate. Anyway, you can remove view using `layoutInstance.removeView(viewInstance);` – Shaishav Sep 22 '16 at 16:34
  • @shaishav I have updated the question could you advise more on how to handle an instance where you are not limiting a user on the number of times to add inputs dynamically,eg: a user adds them ... Most of the resources i have seen use a for loop with initial and final condition pre-set – Geoff Sep 22 '16 at 16:45
  • I guess, you don't need to save the instance. For deleting an inflated layout, you can map the 'Delete' button to call `removeView()` with the current inflated view (of which the button itself is a part of) on the parent `ViewGroup`. Does that make sense? – Shaishav Sep 22 '16 at 16:50
  • The remove part is clear now what about adding multiple items without a limit – Geoff Sep 22 '16 at 16:59
  • 1
    I guess you can keep a layout file with above layout and inflate (and populate) it on each click of the 'Add' button. This should work for any number of views. Whether you need to keep references to each of these inflated layout depends on your use-case. If you strictly need to keep a reference you can save them in an `ArrayList`. – Shaishav Sep 22 '16 at 17:03
  • Okay ill try this – Geoff Sep 22 '16 at 17:08

0 Answers0