okay I will breifly define what I exactly want.I\'m getting an java code(for textview, button,etc) from api like this--> image description here and Xml code(for textview, bu
There two way to add view dynamically .
you create you xml
file .then using LayoutInflater
you lnflater that xml to your view
.
Or You can create view dynamically like var textview=TextView()
then textview.text="xyz"
You need relativelayout or linearlayout where will add those will using addView();
method.
But i condition you have declare your view then you can customize at runtime.
you can check the example
My Dear Friend, Unfortunately, It is not possible. as you are saying XML and java code from serverside. To run java code we need it to be compiled which is not possible on mobile.
Hope you understand. But, what you can do is you can make a helper class which can parse JSON and returns a view which is generated from JSON and JSON contains element description like elementType, width, height, which can be predefined in server.
Or you can use a Webview
that loads a webpage from the server. and to use it in the application. or for some operation use intercept javascript.
Remote code injection is not supported from the very beginning in android. The framework developers may have thought from the security perspective as well as the limited computation that a mobiles have. Please note android studio build process converts these java, xml files into dex files which can't be done in runtime so its not supported.
Important For performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
The alternative approach is to use some JSON format for the views and properties and it will draw the widgets dynamically on a blank canvas will add it to the layout parent.
for example.
{
"type": "LinearLayout",
"orientation": "vertical",
"padding": "16dp",
"children": [{
"layout_width": "200dp",
"gravity": "center",
"type": "TextView",
"text": "@{user.profile.name}"
}, {
"type": "HorizontalProgressBar",
"layout_width": "200dp",
"layout_marginTop": "8dp",
"max": 6000,
"progress": "@{user.profile.experience}"
}]
}
This will add a LinearLayout with Childerns as TextView, HorizontalScrollBar. and data can also be inserted like below.
{
"user": {
"profile": {
"name": "John Doe",
"experience": 4192
}
}
}
On how to use this SDK hit the below link. https://github.com/flipkart-incubator/proteus