0

I was wondering what the proper way of changing things in a layout is. For example, show or don't show some views.

Right now I'm making every button I need in XML and using java to make them VISIBLE or GONE according to the preferences. This works, however I was wondering if this is the best way. I have to make a lot of XML code for the same buttons over and over. Except now they are in different places.

So I was wondering if maybe there are better ways. Would anybody like to enlighten me?

Cheers,

Daan

swapsCAPS
  • 1,481
  • 1
  • 9
  • 10
  • No one can really say without seeing some of your code and knowing exactly when/how you use them. If they are used quite a bit then hide them in java when you need to. If you are able to reuse `Views` or `listeners` then that is best, unless not practical in your situation – codeMagic Apr 12 '13 at 21:48
  • The views I'm using will be used a lot. In this case a large set of buttons. You can choose to put the buttons on the right side of the screen, or the left side of the screen. The easiest way for me was to write the buttons in XML and then set them To VISIBLE or GONE accordingly. – swapsCAPS Apr 12 '13 at 22:36
  • So you are duplicating each one, right and left? If this is what you are doing then you are probably better off looking into fragments but if you have done it and don't have any problems then it should be fine – codeMagic Apr 12 '13 at 22:38
  • Thanks I'll look into fragments soon – swapsCAPS Apr 13 '13 at 00:34

1 Answers1

0

Whenever possible, I do exactly as you suggest. I layout everything and change "visibility" often.

For the majority of my Android development, I reserve dynamic layouts for those situations when you don't know the exact number or types of widgets required on a screen.

BTW: I prefer hand writing the XML instead of using the Graphical Layout Editor. And, I try to use nested "horizontal" and "vertical" LinearLayout cells whenever possible before resorting to the other constructs.

David Manpearl
  • 12,362
  • 8
  • 55
  • 72
  • I have to disagree. `RelativeLayout` was created so nested `LinearLayouts` aren't necessary. They aren't always bad but `RelativeLayout` can be much more powerful and versatile – codeMagic Apr 12 '13 at 22:07
  • I **strongly** disagree with the above comment. `LinearLayout` allows modularity. `RelativeLayout` creates difficult to maintain code and should be used when this is impossible due to desired overlap or other custom behavior, and usually as a component within various other layout objects. `LinearLayout` is also much simpler because you don't have to specify which side, below, above, etc. – David Manpearl Apr 12 '13 at 22:19
  • Oh, I agree that `LinearLayout` is simpler. It's simpler for me to heat up a hotdog for dinner than to make a nice pasta dish. But if thousands of people are going to be enjoying my dinner then I want to give them the pasta. `RelativeLayout` certainly has more of a learning curve but, IMHO, is mush easier to maintain and get the results you desire once you get accustomed to it – codeMagic Apr 12 '13 at 22:32
  • Thank you very much for the info! By dynamic layouts you mean really creating layouts and views from code, without XML? – swapsCAPS Apr 12 '13 at 22:32
  • All of the layout can be done via code as well as XML. Here is a useful example: http://stackoverflow.com/questions/4979212/programmatically-creating-a-relativelayout-in-android – David Manpearl Apr 12 '13 at 22:34