aspose file tools*
The moose likes Android and the fly likes dynamically creating buttons vs creating them through xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "dynamically creating buttons vs creating them through xml" Watch "dynamically creating buttons vs creating them through xml" New topic
Author

dynamically creating buttons vs creating them through xml

utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
Hi

I am new to android programming.
I wish to know as to which is a better way create ui elements like a button. We can do it by wither defingthem in the layout.xml or by adding them at runtime through java like:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));

Can some one explain its pros and cons ?

Thsnks
Utsav.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You should do it in XML whenever possible:

1) The application will be smaller. There is a lot of code required to get a UI properly built, styled, and laid out. When you do it in XML, the XML files will be compressed and all the word done by the Android runtime. This means your deliverable product is smaller and has fewer lines of code. Fewer lines of code generally means fewer errors.

2) It allows greater re-use. You can build a view or part of a view in XML, then re-use it rather easily in different activities, fragments, or other layouts. This isn't impossible to do with code-built UIs, but requires a lot more discipline and foresight.

3) Quicker feedback. When you build with XML you can use the graphical builders to see what you are making and be sure that you get the layout the way you want it as you put it together. If you build it by hand you have to place, compile, run on an emulator to see the results.

4) Easier code to read. GUI code can be a horrible mess with lots of lines, and huge methods. It is hard to debug, hard to read, and hard to modify later on. XML is easier to read and understand. The nested layout of XML closely matches the nested layout of the UI so it is not hard to see what elements are parent/child and what are sibling. Overall just a lot cleaner. Cleaner code means faster development time later on (at least) when it is time to modify the layout, rebrand it, add features, etc...


Steve
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Forgot to mention some of the cons of the XML:

1) You don't have access to all components or information in XML, so sometimes you need to get references in code to add listeners or such to it

2) You can't build dynamic UIs with it, that is, where the number of buttons / fields grow or shrink depending on some values known at runtime. You either have to add the maximum amount number of elements in XML, hide them all, and only show the ones that you need at runtime, or you need to put a placeholder in XML and build the dynamic content at runtime and insert it into the placeholder. Neither one of those things is very tough, but it is a limiting factor.
utsav gupta
Ranch Hand

Joined: Mar 19, 2009
Posts: 57
What an answer!

Thanks Steve!
Regs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamically creating buttons vs creating them through xml