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...