WHY LEAN LAYOUTS? • Complexity ∝ effort to maintain/refactor • Readability • Complex views are hard to parse • Too many → too indented • Stability: a change to one part affects other part 4 MAINTAINABILITY
WHERE’S THE PROBLEM? (HINT: SEVERAL PLACES) r Change in size/position starts measure/layout starting at root. r r r r Some layouts need multiple measure/layout passes, e.g. RelativeLayout Lists make many copies of same layout Deep hierarchies increase complexity and dependency
FIND PROBLEMS • SDK tool for visualizing view hierarchy • Used to be standalone; now in Device Monitor • Do not rely on runtime values for measure(), layout(), and draw() phases • Inaccurate • Chet Haase said so • Best: use on physical device running 4.1 up HIERARCHY VIEWER :
FIND PROBLEMS • systrace: system + application process execution data → interactive reports • Frame rendering → how much time measure/layout takes up; warns of frames that exceed 16.6ms needed for 60fps • dumpsys: collects and dumps “interesting information” about system services status • adb shell dumpsys gfxinfo → perf info related to frames of animation • adb shell dumpsys gfxinfo framestats → detailed frame timing info; on Marshmallow SYSTRACE + DUMPSYS :
FIX PROBLEMS • Lint complains about things that are generally not good ideas. • Good candidates for things that you should fix first. • Don’t nest weights • Remove useless views • Don’t nest too much MAKE LINT HAPPY :
FIX PROBLEMS • Many different ways to do one thing: opt for simplest • Often attributes or other techniques can replace multiple views, e.g.: • TextView compound drawables • Spannable • Placeholder/divider → plain View or Space. • Consolidate styling and spacing → set directly on content layout instead of a wrapper SIMPLIFY AND REDUCE :
FIX PROBLEMS • Many different ways to do one thing: opt for simplest • Often attributes or other techniques can replace multiple views, e.g.: • TextView compound drawables • Spannable • Placeholder/divider → plain View or Space. • Consolidate styling and spacing → set directly on content layout instead of a wrapper SIMPLIFY AND REDUCE :
FIX PROBLEMS • Many different ways to do one thing: opt for simplest • Often attributes or other techniques can replace multiple views, e.g.: • TextView compound drawables • Spannable • Placeholder/divider → plain View or Space. • Consolidate styling and spacing → set directly on content layout instead of a wrapper SIMPLIFY AND REDUCE :
FIX PROBLEMS • Many different ways to do one thing: opt for simplest • Often attributes or other techniques can replace multiple views, e.g.: • TextView compound drawables • Spannable • Placeholder/divider → plain View or Space. • Consolidate styling and spacing → set directly on content layout instead of a wrapper SIMPLIFY AND REDUCE :
FIX PROBLEMS • Sometimes just need to pick the “right” view/layout. • Nested LinearLayouts → RelativeLayout • RelativeLayout to layout n views in a row → LinearLayout • RelativeLayout → FrameLayout + layout_gravity • Do (or at least try) not use RelativeLayout at the root! • Generally, no set rules: “It depends.” SIMPLIFY AND REDUCE :
FIX PROBLEMS • Sometimes just need to pick the “right” view/layout. • Nested LinearLayouts → RelativeLayout • RelativeLayout to layout n views in a row → LinearLayout • RelativeLayout → FrameLayout + layout_gravity • Do (or at least try) not use RelativeLayout at the root! • Generally, no set rules: “It depends.” SIMPLIFY AND REDUCE :
FIX PROBLEMS • Sometimes best option = custom ViewGroup. • Total control over measure/layout logic. • Mitigate double-layout phases. • Also: totally custom Views (draw all the things). • Start out with simple, straightforward layouts: static content, little user interaction • Balance performance gains with development effort. GO CUSTOM :
GOOD PRACTICES • Anticipate and develop good habits • Use simplest solutions where possible • “Don’t do too much stuff.” • Fewer/flatter Views and ViewGroups. • Don’t let problems accumulate. • Best solution? It depends. • Balance performance gains with development effort.