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

4
  • What you want in your layout ? Commented Nov 10, 2016 at 6: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 Commented Nov 10, 2016 at 6:58
  • You must post your code, and ask for help. Commented Nov 10, 2016 at 7:29
  • check out this answer stackoverflow.com/questions/42366353/… Commented Mar 8, 2017 at 18:40

2 Answers 2

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

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

Comments

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
        }
    }

Comments

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.