-2

I want to add UI component in my application dynamically which is received by API. I also want to fetch value of that component on submit. e.g. If there are 3 check box,2 Edittext and 2 radio button, I need to fetch every component's value while click on submit.

Samir Bhatt
  • 3,041
  • 2
  • 25
  • 39
  • Could you clarify the format that these components are being received in? Is the API returning XML strings? – Jack Jun 15 '15 at 11:20
  • API returning json string with type of component and it's que. – Samir Bhatt Jun 15 '15 at 11:22
  • Could you put an example of the JSON in your question? Do you have any control over what the API produces or is it 3rd party? – Jack Jun 15 '15 at 11:25
  • What i want to do is if type= edittext then view of edittext is added, if type= radio then, radio group added. – Samir Bhatt Jun 15 '15 at 11:31

2 Answers2

2

Android's UI widgets, like EditText and RadioGroup, are Java classes. You are welcome to create instances of these using their constructors. You are welcome to configure them using setters. You are welcome to add them to parent containers by calling addView() on the container. And so on.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Android has a JSON parser built in so parsing the JSON is pretty trivial.

Seeing as you know the format, just initiate a JSONObject or JSONArray (Depending on what you need)

JSONObject obj = new JSONObject(jsonString)

Then you can explore the object using the method calls (see docs above)

Once you've parsed the JSON, then its just a case of creating a corresponding instance of each UI component and adding to a view.

Button btn = new Button();
someView.addView(btn);

You can also give components their attributes programmatically. Have a look at the docs to see which methods correspond to which XML attributes.

Jack
  • 2,625
  • 5
  • 33
  • 56