0

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

4
  • You could try and get the properties and attributes of the widgets from database using json ... further dynamically add the views to your app.... Commented 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. Commented Apr 1, 2015 at 7:43
  • @NAVdroid, thats what i am doing right now. but i want to do it in any best approach. Commented Apr 1, 2015 at 7:56
  • XML is read-only file. So you can't edit or create it on runtime. Commented Apr 1, 2015 at 8:16

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

well LayoutInflater creates a View hierarchy step by step, by creating Views and adding them to their parents, so what's the difference if you replicate LayoutInflater's job in your code?
You can, its not impossible. Your performance would absolutely suck though. There's a reason why its not built into Android when it would be trivial for them to do it. And I wouldn't expect a naive implementation thrown together to do very well at all.
if your data structure holding the View hierarchy is more or less as fast as binary xml file used by LayoutInflater there will be absolutely no performance penalty, LayoutInflater really doesn't do any black magic, you can even create the Views by yourself by implementing LayoutInflater.Factory[2]
@syed99 confused? with what?
Are you saying we can implement the layout using LayoutInflater.Factory[2] ,if yes could you provide some example..tqs
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.