2

Are there any tips for cleaning up code that must address many views at once without placing all of the views within some sort of ViewGroup and hence adding layout complexity?

Nesting layouts in Android is costly for performance, but sometimes it can be valuable for cleaning up code. For example, if we have a pseudolayout as such:

<RelativeLayout>
    <View id=A>
    <View id=B>

    <View id=C>
    <View id=D>
</RelativeLayout>

If we want to always show/hide A and B together and C and D together, it would be nice to redesign the layout as such:

<RelativeLayout>
    <LinearLayout id=groupA>
        <View id=A>
        <View id=B>
    </LinearLayout>

    <LinearLayout id=groupB>
        <View id=C>
        <View id=D>
    </LinearLayout>
</RelativeLayout>

This way instead of calling A.setVisibility() and B.setVisibility(), we can call groupA.setVisibility()! This obviously can be much more useful for layouts with many more elements, but... you get the idea.

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58

0 Answers0