As a requirement i need to build ui from database(server),currently i can access the ui components and their properties from db through json and programmtically like
TextView tv=new TextView(mContext);
tv.setText(jsonData.getText());
and able to show the ui. My question is can we create a layout file programmatically so that i can place incoming json meta Data on to that file.
and include this layout xml file. can anybody shed some light on this. thanks
-
You could try and get the properties and attributes of the widgets from database using json ... further dynamically add the views to your app....Navdroid– Navdroid2015-04-01 07:38:25 +00:00Commented Apr 1, 2015 at 7:38
-
You are not the first person to want to do this, and it is an untenable approach. If you want a app whose layout is determined by the web, use a Web page.Booger– Booger2015-04-01 07:43:32 +00:00Commented Apr 1, 2015 at 7:43
-
@NAVdroid, thats what i am doing right now. but i want to do it in any best approach.syed99– syed992015-04-01 07:56:03 +00:00Commented Apr 1, 2015 at 7:56
-
XML is read-only file. So you can't edit or create it on runtime.Anggrayudi H– Anggrayudi H2015-04-01 08:16:13 +00:00Commented Apr 1, 2015 at 8:16
2 Answers
No, you can't create a layout file programatically. Due to the performance issues, Android doesn't support this. The static files are actually pre-compiled for efficiency. YOu can however create a layout programmatically step by step, although it isn't suggested for large layouts.
6 Comments
You can create layout file as any other file. But what for? You will have to save it to SD card or to memory of the phone. Remember that you can't save this file to res and then just call setContentView(int resId); or even assets in Runtime, so setting "on the fly" I think is the best way to implement it