Android library for inflating dynamic layouts in runtime based on JSON configuration fetched from server. Useful in situations when layouts need to change without updating the app.
- Targeting specific devices by brand, model and Android API version
- Layout changes based on configuration fields
- Add repository in root
build.gradle
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
- Add the dependency
dependencies { compile 'com.github.jelic98:dynamico:1.2.0' }
- Create JSON layout and upload it somewhere
{
"views":[
{
"class":"android.widget.ImageView",
"attributes":{
"layout_width":"wrap_content",
"layout_height":"wrap_content",
"src":"http://ecloga.org/projects/dynamico/logo.png",
"cache":true
}
},
{
"class":"android.widget.TextView",
"attributes":{
"text":"Yo!",
"textColor":"#FF69B4"
}
}
]
}- Create XML wrapper layout that will contain loaded views
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/mainLayout"> <!-- Dynamic content will be added here --> </LinearLayout>
- Initialize Dynamico by passing it:
- URL = Link to JSON layout directory
- name = Name of JSON layout file
- layout = Wrapper layout that will contain loaded views
new Dynamico("http://ecloga.org/projects/dynamico", "activity_main", findViewById(R.id.mainLayout)) .initialize();
3b. Initialize Dynamico by passing it:
- JSONStringBuilder = JSON string inside a StringBuilder directory
- name = Name of JSON layout so it can be use for cache porpuses file
- layout = Container layout that will contain the loaded views from the json
StringBuilder sb = new StringBuilder("{\n" + " \"views\":[\n" + " { \n" + " \"class\":\"android.widget.TextView\",\n" + " \"attributes\":{ \n" + " \"text\":\"Sample text\",\n" + " \"textColor\":\"#FF69B4\"\n" + " }\n" + " },\n" + " { \n" + " \"class\":\"android.widget.ImageView\",\n" + " \"attributes\":{\n" + " \"src\": \"https://cdn69.picsart.com/186273671000202.jpg?r1024x1024\"\n" + " }\n" + " }\n" + " ]\n" + "}"); new Dynamico(sb, "activity_main", findViewById(R.id.mainLayout)) .initialize();
For advanced usage, take a look at this awesome manual.
- Event listener
setListener(new LayoutStateListener() { @Override public void onSuccess(String message) { // everything is okay } @Override public void onError(String message) { // notify user } })
- Loading from cache (skip layout fetching from server)
setOptions(CACHE_ONLY)
- Non-stop layout fetching
setOptions(NON_STOP) // use with setAsyncPause(long millis)
One and only constructor
- Parameters:
url— URL of directory where JSON layout file is located (for example, "http://ecloga.org/projects/dynamico")name— JSON layout file name with or without extension (for example, "activity_main")layout— wrapper layout that will contain inflated layout from JSON file (for example, findViewById(R.id.mainLayout))
- Exceptions:
DynamicoException— if any of passed parameters is null
Attaches event listener to Dynamico object
- Parameters:
listener— listener for success and error events caused by network, storage, etc. - Returns: Dynamico object ready for initialization
Attaches options to Dynamico object
- Parameters:
options— options for Dynamico (for example, ONLY_CACHE) - Returns: Dynamico object ready for initialization
Starts layout fetching from cache/server depending on provided options
- Support vector drawables
- Support more views