Is it possible to render a dynamic layout on Android which received via API?

1.3k views Asked by At

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.

2

There are 2 answers

0
CommonsWare On BEST ANSWER

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.

0
Jack On

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.