3
{
  layout:[
   {
    tag :"edittext",
    name :"Name",
    hint :"Type your name here"
   },
   {
    tag :"checkbox",
    name :"Is married",
    hint :""
   },
   {
    tag :"button",
    name :"Submit",
    hint :""
   }
 ]
}

I am describing what exactly i want . First of all the above json will change every time. Structure will same , just tag, name and hint value will change . Now the above jsonarray has three jsonobject . it might be any number(4/5/6 any number of jsonobject) . Can any one plz suggest me how to solve this issue ? Thanks

anu
  • 213
  • 1
  • 3
  • 10
  • What you want in your layout ? – Deepak Sachdeva Nov 10 '16 at 06:57
  • How do you define the position of each view if u want it dynamically. In other case, I believe it is straight forward to create the views by loop parsing the json array and create views to the parent. It is a matter of parsing tag and create respective view with new constructor.Attach the same to the view – rafa Nov 10 '16 at 06:58
  • You must post your code, and ask for help. – Adnan Bin Mustafa Nov 10 '16 at 07:29
  • check out this answer http://stackoverflow.com/questions/42366353/is-it-possible-to-set-xml-layout-as-a-view-not-from-resources-dynamically/42678791#42678791 – vader Mar 08 '17 at 18:40

2 Answers2

1

Its better to divide your question in small small parts, so here is something i have for your

  1. Parse JSON and fetch its value : I suggest you to follow How to parse JSON in Android this link and you will have everything from your json.
  2. Check whether control is EditText or Button or anything else : I will suggest you to use switch case for better coding structure

    switch (tag ){
        case "edittext":
            //add edittext
            break;
        case "button":
            //add button
            break;
    }
    

follow this links to add controls dynamically

Generating Edit Text Programatically in android
Add button to a layout programmatically
How to add checkboxes dynamically in android

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

You can create your layout according to the size of jsonarray.

JSONArray jArray = jObject.getJSONArray("layout");
    layout = (LinearLayout) findViewById(R.id.statsviewlayout);

    for (int i=0; i < jArray.length(); i++)
    {
        try {
            JSONObject oneObject = jArray.getJSONObject(i);
            // Pulling items from the array
            String tag = oneObject.getString("tag");
            String name= oneObject.getString("name");
            String hint= oneObject.getString("hint");
            Button button = new Button(this);
            button.setText(name);
            button.setLayoutParams(new LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        layout.addView(buyButton);

        } catch (JSONException e) {
            // Oops
        }
    }
Deepak Sachdeva
  • 950
  • 8
  • 16