Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solution to build dynamic forms with Android

I am actually developing an Android application on which I should display dynamic forms based on metadata contained inside JSON documents. Basically the way it works (without the details) is that a JSON document represent the structure of a form:

{
    "fields": [
        {
            "name": "fieldA",
            "type": "STRING",
            "minCharacters": 10,
            "maxCharacters": 100
        },
        {
            "name": "fieldB",
            "type": "INTEGER",
            "min": 10,
            "max": 100
        },
        {
            "name": "fieldC",
            "type": "BOOLEAN_CHECKBOX",
            "defaultValue": true
        }
        ...
    ],
    "name": "Form A"
}

What I'm doing actually when the application receive one of those JSON documents is that it loop through each fields and parse it to the appropriate view (EditText, Checkbox, custom view, etc.), adding a tag to the view (to be able to retrieve it easily) and add the view to a LinearLayout. Here is a pseudo-code of how it is working actually:

//Add form title
linearLayout.addView(new TextView(form.name));
//Add form fields
for(Field field: form.fields) {
    View view;
    switch(field.type){
        case STRING: view = new EditText();
        ...
    }
    view.setTag(field.id);
    linearLayout.addView(view);
}

The issue with this is that with large forms (like >20 fields), it need to inflate lot of views and the UI thread suffer a lot. Another point to take into account is that a single screen may have multiple forms (one after another vertically sorted).

To avoid overloading the UI thread I thought of 2 possible solutions:

  1. Using a RecyclerView.
  2. Using Litho by Facebook.

But multiple questions comes to me when considering these 2 solutions:

  1. Is it a good use case to use Litho? Or using a RecyclerView is enough?
  2. What about the state of my views? If I use a Recycling pattern, would I be able to keep the state of each of my fields (even those off-screen) and so being able to save the form without losing data?
  3. If I use a Recycling pattern to display one form, how would I handle multiple forms? Can we have nested RecyclerView? Forms need to be displayed one after another like inside a vertical RV but if forms themselves are RV, how should I handle this?

This is more a "good practice" question and giving the right way or one of the right way of achieving my goal than a need of a specific answer with code example, etc.

Thank's in advance for your time.

like image 710
MHogge Avatar asked Jul 12 '17 15:07

MHogge


People also ask

What Builder is used to create dynamic forms?

Make dynamic forms, and share or embed them in your site with Jotform's dynamic form builder — no coding required! Set up conditional logic, data verification, pre-populated form fields, and more in a few easy clicks.

What is a dynamic webform?

Act-On's dynamic forms allow you to collect, store, and transfer crucial lead data. And with progressive profiling, you can ask repeat convertors new questions to gather additional information and personalize their journey.


1 Answers

When architecting for the mobile application I would like to address the following questions:

Is it a good use case to use Litho? Or using a RecyclerView is enough?

  1. Are the views are being recycled properly: What does it mean to us is consider, creating 40-50 view per screen and as user moves out of the view, system should not mark all views for GC rather it should be inside some kind archived list and as we require it again we should be able to fetch from it.

    Why do we need to that: GC is the costliest operation which would cause app rendering to be jitter, we try to minimize the GC to called at this point by not clearing the views

    For this I would like to go with litho, justification is here as your requirement seems to have more of variable count of viewtypesreference

    Conclusion: Litho +1, RecyclerView +0

What about the state of my views? If I use a Recycling pattern, would I be able to keep the state of each of my fields (even those off-screen) and so being able to save the form without losing data?

  1. Saving EditText content in RecyclerView This is one the component but same logic should be appliced to checkbox or radiobutton as well. or as in state-maintenance for litho is here

    Conclusion: Litho +1, RecyclerView +1 both has specific API's to achieve state maintenance

If I use a Recycling pattern to display one form, how would I handle multiple forms? Can we have nested RecyclerView? Forms need to be displayed one after another like inside a vertical RV but if forms themselves are RV, how should I handle this?

  1. This has to be addressed with user experience plus technical capability: As per user behaviour IMHO,I discourage the nested vertical scroll however others were able to achieve it you can easily find on how to in SO. Optimal solution would be to have horizontal scroll within either Andriod's or litho's recycler view as here

NOTE: If you need to know implementation details, please raise it as separate question, I would be happy to help there


UPDATE #1:

The issue with this is that with large forms (like >20 fields), it need to inflate lot of views and the UI thread suffer a lot.

UI creation/layout has to be performed at the backend only adding to the view has to be done on UI thread. And litho does it in-built. However same can be achieved native recycler view as well but you have to move off the UI thread and post periodically to UI.


like image 177
Mani Avatar answered Sep 18 '22 22:09

Mani