228

I was just building some UI in xml, and Lint gave me a warning and said to set android:baselineAligned to false to improve performance in ListView.

The docs for the Lint changes that added this warning say

Layout performance: Finds LinearLayouts with weights where you should set android:baselineAligned="false" for better performance, and also finds cases where you have nested weights which can cause performance problems.

Can somebody explain why this improves performance, specifically when weight is involved?

Bananeweizen
  • 21,797
  • 8
  • 68
  • 88
Christopher Perry
  • 38,891
  • 43
  • 145
  • 187

3 Answers3

175

By setting android:baselineAligned="false" , you're preventing the extra work your app's layout has to do in order to Align its children's baselines; which can obviously increase the performance. (Fewer unnecessary operations on UI => Better performance)

Clyde
  • 7,389
  • 5
  • 31
  • 57
Nima
  • 6,383
  • 7
  • 46
  • 68
  • 23
    That part is obvious. What I was trying to get at is why is this only a performance improvement when weight is involved? – Christopher Perry Feb 17 '12 at 02:02
  • 3
    Because linearlayout only need to calculate the baseline when weights are involved. Its like a smart feature which actually can be annoying. – Warpzit Feb 23 '12 at 08:33
  • 52
    The term baseline comes from typography. It's the invisible line letters in text sit on. http://en.wikipedia.org/wiki/Baseline_%28typography%29 – Zsolt Safrany Sep 29 '12 at 12:26
  • 12
    So if it's a performance improvement, that means work that's normally done isn't being done anymore. What are the side-effects of this? Does the work that we're now avoiding have the capability of yielding different results? – lhunath Mar 26 '13 at 20:29
  • 31
    @lhunath according to the documentation, The default is set to **true**. this forces the baseline of any UI text to align themselves with eachother. For example think about buttons,you have like two buttons with same width/height on the same line but one of them have a longer text, because of `baselineAlined="true"`, the button with longer text can end up moving around to align it's text with other buttons. For a better example, check out [this post](http://udinic.wordpress.com/tag/baselinealigned/) – Nima Mar 27 '13 at 16:11
  • 8
    Here is the visual solution! http://stackoverflow.com/a/38007282/2959200 – Sjd Sep 27 '16 at 06:46
  • @lhunath yes it can result in different layout as I've experienced cases where I discovered unexpectedly that the baseline is not just a property of text views but is also calculated for the layout that wraps them, example would be LinearLayout containing two RelativeLayouts each containing a Button. The RelativeLayouts calculate a baseline from their contents, you will still get the warning but the layout changes if you make the change. – Robert Tuck Feb 24 '17 at 11:51
  • Another nice article with visual explanation: https://medium.com/@pareshmayani/androiddev-tip-set-android-baselinealigned-false-on-this-element-for-better-performance-810fa3148043 – Jérémy Reynaud Jan 31 '19 at 11:23
22

how android:baselineAligned="false" help . It may not be the answer but help to get concept.

I've just managed to get 3 items (icon, text, button) centered vertically in horizontal LinearLayout.

This may seem simple, but in reality specifying android:gravity="center_vertical" as LinearLayout attribute is not enough - icon is centered, but text and button are not. This is because (presumably) text have a baseline, and centering algorithm uses it instead of 'real' vertical center. But what is worse - button (which comes next to text) is centered using text's baseline!

Specifying android:baselineAligned="false" in LinearLayout turns this off, and everything centers correctly.

Joffrey
  • 32,348
  • 6
  • 68
  • 100
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
17
// Baseline alignment requires to measure widgets to obtain the
                // baseline offset (in particular for TextViews). The following
                // defeats the optimization mentioned above. Allow the child to
                // use as much space as it wants because we can shrink things
                // later (and re-measure).
                if (baselineAligned) {
                    final int freeSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                    child.measure(freeSpec, freeSpec);
                }

https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/LinearLayout.java#L1093

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
boiledwater
  • 10,372
  • 4
  • 37
  • 38