How to implement Dynamic Views in Android ?

One of the challenges of being an Android developer is to minimise unnecessary app updates . Releasing updates for small UI changes hurts a bit.

Imagine a situation where

— A view in your app has to be changed dynamically from time to time.

— A new component has to be added in a layout.

The common practice to handle the above situations is to make respective changes and release an update .

This post will try to throw some light on a technique which can update Android native layouts on the go without releasing any updates.

json2view is the best open source library which aims to solve this problem. It can convert a compatible json file to an Android view which can be loaded dynamically. It also provides a functionality to convert an xml layout to a compatible json file.

A sample code of a layout before and after converting it to a json.

A simple edittext in a linear layout
Layout after converting to json

The library in client side understands the json and converts it to a view. The conversion happens using java reflection. Custom views present in the app can also be loaded with little fuss.

if (!widget.contains(".")) {
widget = "android.widget." + widget;
Class viewClass = Class.forName(widget);
view = (View) viewClass.getConstructor(Context.class).newInstance(new Object[]{context});

All you need to do is add a couple of lines in your activity/fragment to convert json object to a view and boom the layout can be updated anytime.

ViewParent parent = (ViewParent)findViewById(;
JSONObject jsonObject = ... // load from network, sdcard etc
View sampleView = DynamicView.createView(this, jsonObject, parent);

I would like to suggest this library for a starter with a basic requirement and would not like to invest much time to solve this problem.

If performance matters a lot to you and your use case is not just to display views and had to do something with the views, this library is not a perfect fit. I will go more detailed into a few solutions to overcome the cons.
But a lot can be learned from the approach used here — create a json with view properties , write code in your app which can understand the json and convert it to a view and do what ever else you want.

Rolling up custom solutions

One of the main drawback of the library is the performance hit to load a complex layout( Imagine the number of reflections taking place).

Since each app has its own design mantra and custom views, an ideal solution is to use custom views and avoid reflection while creating these views.

Adding custom view can also reduce the payload by a huge margin as they generally have default properties.

What about listeners?

Adding listeners crosses the mind of a developer after displaying views.This can be solved by creating a listening layer on top of this which can listen to and perform common actions in the app.

For example “action”:”date” can open a date picker.

This layer can also take care of data binding between views.

Understanding the requirements and predicting the use cases will play a huge role to create contracts for actions, data binding and create a custom wrapper to this library which can do magic to your app.