Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dynamically creating buttons vs creating them through xml

 
utsav gupta
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What an answer!

Thanks Steve!
Regs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic