blob: fd3dc037d77fcbcb7ec9506cc65752341aa94c5d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.widget;
18
Romain Guy6876b4f2013-06-03 14:25:56 -070019import android.util.ArrayMap;
Fabrice Di Meglio9e3b0022011-06-06 16:30:29 -070020import com.android.internal.R;
21
Romain Guybc5d8762012-01-06 16:40:49 -080022import java.util.ArrayDeque;
Fabrice Di Meglio9e3b0022011-06-06 16:30:29 -070023import java.util.ArrayList;
24import java.util.Comparator;
Fabrice Di Meglio9e3b0022011-06-06 16:30:29 -070025import java.util.SortedSet;
26import java.util.TreeSet;
27
svetoslavganov75986cf2009-05-14 22:28:01 -070028import android.content.Context;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070029import android.content.res.TypedArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070030import android.graphics.Rect;
Adam Powell7da4b732012-12-07 15:28:33 -080031import android.os.Build;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.util.AttributeSet;
Romain Guy6876b4f2013-06-03 14:25:56 -070033import android.util.Pools.SynchronizedPool;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070034import android.util.SparseArray;
svetoslavganov75986cf2009-05-14 22:28:01 -070035import android.view.Gravity;
36import android.view.View;
37import android.view.ViewDebug;
38import android.view.ViewGroup;
39import android.view.accessibility.AccessibilityEvent;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080040import android.view.accessibility.AccessibilityNodeInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070041import android.widget.RemoteViews.RemoteView;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070042
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -080043import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
45/**
46 * A Layout where the positions of the children can be described in relation to each other or to the
Romain Guy520c4202010-01-04 10:56:38 -080047 * parent.
Romain Guya1f3e4a2009-06-04 15:10:46 -070048 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 * <p>
50 * Note that you cannot have a circular dependency between the size of the RelativeLayout and the
51 * position of its children. For example, you cannot have a RelativeLayout whose height is set to
52 * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT WRAP_CONTENT} and a child set to
53 * {@link #ALIGN_PARENT_BOTTOM}.
54 * </p>
Romain Guya1f3e4a2009-06-04 15:10:46 -070055 *
Adam Powell2c8cc972012-12-07 18:04:51 -080056 * <p><strong>Note:</strong> In platform version 17 and lower, RelativeLayout was affected by
57 * a measurement bug that could cause child views to be measured with incorrect
58 * {@link android.view.View.MeasureSpec MeasureSpec} values. (See
59 * {@link android.view.View.MeasureSpec#makeMeasureSpec(int, int) MeasureSpec.makeMeasureSpec}
60 * for more details.) This was triggered when a RelativeLayout container was placed in
61 * a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view
62 * not equipped to properly measure with the MeasureSpec mode
63 * {@link android.view.View.MeasureSpec#UNSPECIFIED UNSPECIFIED} was placed in a RelativeLayout,
64 * this would silently work anyway as RelativeLayout would pass a very large
65 * {@link android.view.View.MeasureSpec#AT_MOST AT_MOST} MeasureSpec instead.</p>
66 *
67 * <p>This behavior has been preserved for apps that set <code>android:targetSdkVersion="17"</code>
68 * or older in their manifest's <code>uses-sdk</code> tag for compatibility. Apps targeting SDK
69 * version 18 or newer will receive the correct behavior</p>
70 *
Scott Main4c359b72012-07-24 15:51:27 -070071 * <p>See the <a href="{@docRoot}guide/topics/ui/layout/relative.html">Relative
72 * Layout</a> guide.</p>
Scott Main41ec6532010-08-19 16:57:07 -070073 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 * <p>
75 * Also see {@link android.widget.RelativeLayout.LayoutParams RelativeLayout.LayoutParams} for
76 * layout attributes
77 * </p>
Romain Guya1f3e4a2009-06-04 15:10:46 -070078 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * @attr ref android.R.styleable#RelativeLayout_gravity
80 * @attr ref android.R.styleable#RelativeLayout_ignoreGravity
81 */
82@RemoteView
83public class RelativeLayout extends ViewGroup {
84 public static final int TRUE = -1;
85
86 /**
87 * Rule that aligns a child's right edge with another child's left edge.
88 */
89 public static final int LEFT_OF = 0;
90 /**
91 * Rule that aligns a child's left edge with another child's right edge.
92 */
93 public static final int RIGHT_OF = 1;
94 /**
95 * Rule that aligns a child's bottom edge with another child's top edge.
96 */
97 public static final int ABOVE = 2;
98 /**
99 * Rule that aligns a child's top edge with another child's bottom edge.
100 */
101 public static final int BELOW = 3;
102
103 /**
104 * Rule that aligns a child's baseline with another child's baseline.
105 */
106 public static final int ALIGN_BASELINE = 4;
107 /**
108 * Rule that aligns a child's left edge with another child's left edge.
109 */
110 public static final int ALIGN_LEFT = 5;
111 /**
112 * Rule that aligns a child's top edge with another child's top edge.
113 */
114 public static final int ALIGN_TOP = 6;
115 /**
116 * Rule that aligns a child's right edge with another child's right edge.
117 */
118 public static final int ALIGN_RIGHT = 7;
119 /**
120 * Rule that aligns a child's bottom edge with another child's bottom edge.
121 */
122 public static final int ALIGN_BOTTOM = 8;
123
124 /**
125 * Rule that aligns the child's left edge with its RelativeLayout
126 * parent's left edge.
127 */
128 public static final int ALIGN_PARENT_LEFT = 9;
129 /**
130 * Rule that aligns the child's top edge with its RelativeLayout
131 * parent's top edge.
132 */
133 public static final int ALIGN_PARENT_TOP = 10;
134 /**
135 * Rule that aligns the child's right edge with its RelativeLayout
136 * parent's right edge.
137 */
138 public static final int ALIGN_PARENT_RIGHT = 11;
139 /**
140 * Rule that aligns the child's bottom edge with its RelativeLayout
141 * parent's bottom edge.
142 */
143 public static final int ALIGN_PARENT_BOTTOM = 12;
144
145 /**
146 * Rule that centers the child with respect to the bounds of its
147 * RelativeLayout parent.
148 */
149 public static final int CENTER_IN_PARENT = 13;
150 /**
151 * Rule that centers the child horizontally with respect to the
152 * bounds of its RelativeLayout parent.
153 */
154 public static final int CENTER_HORIZONTAL = 14;
155 /**
156 * Rule that centers the child vertically with respect to the
157 * bounds of its RelativeLayout parent.
158 */
159 public static final int CENTER_VERTICAL = 15;
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700160 /**
161 * Rule that aligns a child's end edge with another child's start edge.
162 */
163 public static final int START_OF = 16;
164 /**
165 * Rule that aligns a child's start edge with another child's end edge.
166 */
167 public static final int END_OF = 17;
168 /**
169 * Rule that aligns a child's start edge with another child's start edge.
170 */
171 public static final int ALIGN_START = 18;
172 /**
173 * Rule that aligns a child's end edge with another child's end edge.
174 */
175 public static final int ALIGN_END = 19;
176 /**
177 * Rule that aligns the child's start edge with its RelativeLayout
178 * parent's start edge.
179 */
180 public static final int ALIGN_PARENT_START = 20;
181 /**
182 * Rule that aligns the child's end edge with its RelativeLayout
183 * parent's end edge.
184 */
185 public static final int ALIGN_PARENT_END = 21;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700187 private static final int VERB_COUNT = 22;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Romain Guybc5d8762012-01-06 16:40:49 -0800189
190 private static final int[] RULES_VERTICAL = {
191 ABOVE, BELOW, ALIGN_BASELINE, ALIGN_TOP, ALIGN_BOTTOM
192 };
193
194 private static final int[] RULES_HORIZONTAL = {
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700195 LEFT_OF, RIGHT_OF, ALIGN_LEFT, ALIGN_RIGHT, START_OF, END_OF, ALIGN_START, ALIGN_END
Romain Guybc5d8762012-01-06 16:40:49 -0800196 };
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 private View mBaselineView = null;
199 private boolean mHasBaselineAlignedChild;
200
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700201 private int mGravity = Gravity.START | Gravity.TOP;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 private final Rect mContentBounds = new Rect();
203 private final Rect mSelfBounds = new Rect();
204 private int mIgnoreGravity;
205
Romain Guy725015a2009-06-23 14:27:34 -0700206 private SortedSet<View> mTopToBottomLeftToRightSet = null;
207
208 private boolean mDirtyHierarchy;
Romain Guy6876b4f2013-06-03 14:25:56 -0700209 private View[] mSortedHorizontalChildren;
210 private View[] mSortedVerticalChildren;
Romain Guy725015a2009-06-23 14:27:34 -0700211 private final DependencyGraph mGraph = new DependencyGraph();
svetoslavganov75986cf2009-05-14 22:28:01 -0700212
Adam Powell7da4b732012-12-07 15:28:33 -0800213 // Compatibility hack. Old versions of the platform had problems
214 // with MeasureSpec value overflow and RelativeLayout was one source of them.
215 // Some apps came to rely on them. :(
216 private boolean mAllowBrokenMeasureSpecs = false;
Romain Guy76d59a32013-04-08 10:51:35 -0700217 // Compatibility hack. Old versions of the platform would not take
218 // margins and padding into account when generating the height measure spec
219 // for children during the horizontal measure pass.
220 private boolean mMeasureVerticalWithPaddingMargin = false;
Adam Powell7da4b732012-12-07 15:28:33 -0800221
Fabrice Di Megliod5ffc792013-02-19 15:59:21 -0800222 // A default width used for RTL measure pass
Philip Milneca2e9e12013-04-22 12:44:29 -0700223 /**
224 * Value reduced so as not to interfere with View's measurement spec. flags. See:
225 * {@link View#MEASURED_SIZE_MASK}.
226 * {@link View#MEASURED_STATE_TOO_SMALL}.
227 **/
Philip Milne3c647d22013-04-23 14:31:23 -0700228 private static final int DEFAULT_WIDTH = 0x00010000;
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 public RelativeLayout(Context context) {
231 super(context);
Romain Guy76d59a32013-04-08 10:51:35 -0700232 queryCompatibilityModes(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 }
234
235 public RelativeLayout(Context context, AttributeSet attrs) {
236 super(context, attrs);
237 initFromAttributes(context, attrs);
Romain Guy76d59a32013-04-08 10:51:35 -0700238 queryCompatibilityModes(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 }
240
241 public RelativeLayout(Context context, AttributeSet attrs, int defStyle) {
242 super(context, attrs, defStyle);
243 initFromAttributes(context, attrs);
Romain Guy76d59a32013-04-08 10:51:35 -0700244 queryCompatibilityModes(context);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 }
246
247 private void initFromAttributes(Context context, AttributeSet attrs) {
248 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RelativeLayout);
249 mIgnoreGravity = a.getResourceId(R.styleable.RelativeLayout_ignoreGravity, View.NO_ID);
250 mGravity = a.getInt(R.styleable.RelativeLayout_gravity, mGravity);
251 a.recycle();
252 }
253
Romain Guy76d59a32013-04-08 10:51:35 -0700254 private void queryCompatibilityModes(Context context) {
255 int version = context.getApplicationInfo().targetSdkVersion;
256 mAllowBrokenMeasureSpecs = version <= Build.VERSION_CODES.JELLY_BEAN_MR1;
257 mMeasureVerticalWithPaddingMargin = version >= Build.VERSION_CODES.JELLY_BEAN_MR2;
258 }
259
Patrick Dubroye0a799a2011-05-04 16:19:22 -0700260 @Override
261 public boolean shouldDelayChildPressedState() {
262 return false;
263 }
264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 /**
266 * Defines which View is ignored when the gravity is applied. This setting has no
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700267 * effect if the gravity is <code>Gravity.START | Gravity.TOP</code>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 *
269 * @param viewId The id of the View to be ignored by gravity, or 0 if no View
270 * should be ignored.
271 *
272 * @see #setGravity(int)
273 *
274 * @attr ref android.R.styleable#RelativeLayout_ignoreGravity
275 */
276 @android.view.RemotableViewMethod
277 public void setIgnoreGravity(int viewId) {
278 mIgnoreGravity = viewId;
279 }
280
281 /**
Philip Milne1018fb42012-03-13 12:00:04 -0700282 * Describes how the child views are positioned.
283 *
284 * @return the gravity.
285 *
286 * @see #setGravity(int)
287 * @see android.view.Gravity
288 *
289 * @attr ref android.R.styleable#RelativeLayout_gravity
290 */
291 public int getGravity() {
292 return mGravity;
293 }
294
295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 * Describes how the child views are positioned. Defaults to
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700297 * <code>Gravity.START | Gravity.TOP</code>.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 *
Adam Powell1fec24e2011-09-15 14:19:30 -0700299 * <p>Note that since RelativeLayout considers the positioning of each child
300 * relative to one another to be significant, setting gravity will affect
301 * the positioning of all children as a single unit within the parent.
302 * This happens after children have been relatively positioned.</p>
303 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 * @param gravity See {@link android.view.Gravity}
305 *
306 * @see #setHorizontalGravity(int)
307 * @see #setVerticalGravity(int)
308 *
309 * @attr ref android.R.styleable#RelativeLayout_gravity
310 */
311 @android.view.RemotableViewMethod
312 public void setGravity(int gravity) {
313 if (mGravity != gravity) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700314 if ((gravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) == 0) {
Fabrice Di Meglio9e3b0022011-06-06 16:30:29 -0700315 gravity |= Gravity.START;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 }
317
318 if ((gravity & Gravity.VERTICAL_GRAVITY_MASK) == 0) {
319 gravity |= Gravity.TOP;
320 }
321
322 mGravity = gravity;
323 requestLayout();
324 }
325 }
326
327 @android.view.RemotableViewMethod
328 public void setHorizontalGravity(int horizontalGravity) {
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700329 final int gravity = horizontalGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
330 if ((mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) != gravity) {
331 mGravity = (mGravity & ~Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) | gravity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 requestLayout();
333 }
334 }
335
336 @android.view.RemotableViewMethod
337 public void setVerticalGravity(int verticalGravity) {
338 final int gravity = verticalGravity & Gravity.VERTICAL_GRAVITY_MASK;
339 if ((mGravity & Gravity.VERTICAL_GRAVITY_MASK) != gravity) {
340 mGravity = (mGravity & ~Gravity.VERTICAL_GRAVITY_MASK) | gravity;
341 requestLayout();
342 }
343 }
344
345 @Override
346 public int getBaseline() {
347 return mBaselineView != null ? mBaselineView.getBaseline() : super.getBaseline();
348 }
349
350 @Override
Romain Guy725015a2009-06-23 14:27:34 -0700351 public void requestLayout() {
352 super.requestLayout();
353 mDirtyHierarchy = true;
354 }
355
356 private void sortChildren() {
Romain Guy6876b4f2013-06-03 14:25:56 -0700357 final int count = getChildCount();
358 if (mSortedVerticalChildren == null || mSortedVerticalChildren.length != count) {
359 mSortedVerticalChildren = new View[count];
360 }
361
362 if (mSortedHorizontalChildren == null || mSortedHorizontalChildren.length != count) {
363 mSortedHorizontalChildren = new View[count];
364 }
Romain Guy725015a2009-06-23 14:27:34 -0700365
366 final DependencyGraph graph = mGraph;
367 graph.clear();
368
369 for (int i = 0; i < count; i++) {
Romain Guy6876b4f2013-06-03 14:25:56 -0700370 graph.add(getChildAt(i));
Romain Guy725015a2009-06-23 14:27:34 -0700371 }
372
Romain Guybc5d8762012-01-06 16:40:49 -0800373 graph.getSortedViews(mSortedVerticalChildren, RULES_VERTICAL);
374 graph.getSortedViews(mSortedHorizontalChildren, RULES_HORIZONTAL);
Romain Guy725015a2009-06-23 14:27:34 -0700375 }
376
377 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Romain Guy725015a2009-06-23 14:27:34 -0700379 if (mDirtyHierarchy) {
380 mDirtyHierarchy = false;
381 sortChildren();
382 }
383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 int myWidth = -1;
385 int myHeight = -1;
386
387 int width = 0;
388 int height = 0;
389
Adam Powell132a7422012-09-30 12:39:56 -0700390 final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
391 final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
392 final int widthSize = MeasureSpec.getSize(widthMeasureSpec);
393 final int heightSize = MeasureSpec.getSize(heightMeasureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394
395 // Record our dimensions if they are known;
396 if (widthMode != MeasureSpec.UNSPECIFIED) {
397 myWidth = widthSize;
398 }
399
400 if (heightMode != MeasureSpec.UNSPECIFIED) {
401 myHeight = heightSize;
402 }
403
404 if (widthMode == MeasureSpec.EXACTLY) {
405 width = myWidth;
406 }
407
408 if (heightMode == MeasureSpec.EXACTLY) {
409 height = myHeight;
410 }
411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 mHasBaselineAlignedChild = false;
413
414 View ignore = null;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700415 int gravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK;
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700416 final boolean horizontalGravity = gravity != Gravity.START && gravity != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 gravity = mGravity & Gravity.VERTICAL_GRAVITY_MASK;
418 final boolean verticalGravity = gravity != Gravity.TOP && gravity != 0;
419
420 int left = Integer.MAX_VALUE;
421 int top = Integer.MAX_VALUE;
422 int right = Integer.MIN_VALUE;
423 int bottom = Integer.MIN_VALUE;
424
Romain Guyf7dabb02009-06-25 14:47:14 -0700425 boolean offsetHorizontalAxis = false;
426 boolean offsetVerticalAxis = false;
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 if ((horizontalGravity || verticalGravity) && mIgnoreGravity != View.NO_ID) {
429 ignore = findViewById(mIgnoreGravity);
430 }
431
Romain Guyf7dabb02009-06-25 14:47:14 -0700432 final boolean isWrapContentWidth = widthMode != MeasureSpec.EXACTLY;
433 final boolean isWrapContentHeight = heightMode != MeasureSpec.EXACTLY;
434
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800435 // We need to know our size for doing the correct computation of children positioning in RTL
436 // mode but there is no practical way to get it instead of running the code below.
Fabrice Di Megliod5ffc792013-02-19 15:59:21 -0800437 // So, instead of running the code twice, we just set the width to a "default display width"
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800438 // before the computation and then, as a last pass, we will update their real position with
Fabrice Di Megliod5ffc792013-02-19 15:59:21 -0800439 // an offset equals to "DEFAULT_WIDTH - width".
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800440 final int layoutDirection = getLayoutDirection();
441 if (isLayoutRtl() && myWidth == -1) {
Fabrice Di Megliod5ffc792013-02-19 15:59:21 -0800442 myWidth = DEFAULT_WIDTH;
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800443 }
444
Romain Guye24ef602009-06-25 13:01:55 -0700445 View[] views = mSortedHorizontalChildren;
Romain Guy725015a2009-06-23 14:27:34 -0700446 int count = views.length;
Fabrice Di Meglio13705ed2012-11-26 20:05:31 -0800447
Romain Guy725015a2009-06-23 14:27:34 -0700448 for (int i = 0; i < count; i++) {
449 View child = views[i];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 if (child.getVisibility() != GONE) {
451 LayoutParams params = (LayoutParams) child.getLayoutParams();
Fabrice Di Meglio54726132013-01-18 18:36:45 -0800452 int[] rules = params.getRules(layoutDirection);
Romain Guy95607032009-06-24 14:37:03 -0700453
Fabrice Di Meglio54726132013-01-18 18:36:45 -0800454 applyHorizontalSizeRules(params, myWidth, rules);
Romain Guyf782e602009-06-25 15:26:49 -0700455 measureChildHorizontal(child, params, myWidth, myHeight);
Fabrice Di Meglio54726132013-01-18 18:36:45 -0800456
Romain Guyf7dabb02009-06-25 14:47:14 -0700457 if (positionChildHorizontal(child, params, myWidth, isWrapContentWidth)) {
458 offsetHorizontalAxis = true;
459 }
Romain Guy725015a2009-06-23 14:27:34 -0700460 }
461 }
462
Romain Guye24ef602009-06-25 13:01:55 -0700463 views = mSortedVerticalChildren;
Romain Guy725015a2009-06-23 14:27:34 -0700464 count = views.length;
Romain Guyf7dabb02009-06-25 14:47:14 -0700465
Romain Guy725015a2009-06-23 14:27:34 -0700466 for (int i = 0; i < count; i++) {
467 View child = views[i];
468 if (child.getVisibility() != GONE) {
469 LayoutParams params = (LayoutParams) child.getLayoutParams();
Romain Guy95607032009-06-24 14:37:03 -0700470
471 applyVerticalSizeRules(params, myHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 measureChild(child, params, myWidth, myHeight);
Romain Guyf7dabb02009-06-25 14:47:14 -0700473 if (positionChildVertical(child, params, myHeight, isWrapContentHeight)) {
474 offsetVerticalAxis = true;
475 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476
Romain Guyf7dabb02009-06-25 14:47:14 -0700477 if (isWrapContentWidth) {
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800478 if (isLayoutRtl()) {
479 width = Math.max(width, myWidth - params.mLeft);
480 } else {
481 width = Math.max(width, params.mRight);
482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Romain Guyf7dabb02009-06-25 14:47:14 -0700484
485 if (isWrapContentHeight) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 height = Math.max(height, params.mBottom);
487 }
488
489 if (child != ignore || verticalGravity) {
490 left = Math.min(left, params.mLeft - params.leftMargin);
491 top = Math.min(top, params.mTop - params.topMargin);
492 }
493
494 if (child != ignore || horizontalGravity) {
495 right = Math.max(right, params.mRight + params.rightMargin);
496 bottom = Math.max(bottom, params.mBottom + params.bottomMargin);
497 }
498 }
499 }
500
501 if (mHasBaselineAlignedChild) {
Romain Guy725015a2009-06-23 14:27:34 -0700502 for (int i = 0; i < count; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 View child = getChildAt(i);
504 if (child.getVisibility() != GONE) {
505 LayoutParams params = (LayoutParams) child.getLayoutParams();
506 alignBaseline(child, params);
507
508 if (child != ignore || verticalGravity) {
Romain Guy725015a2009-06-23 14:27:34 -0700509 left = Math.min(left, params.mLeft - params.leftMargin);
510 top = Math.min(top, params.mTop - params.topMargin);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 }
512
513 if (child != ignore || horizontalGravity) {
514 right = Math.max(right, params.mRight + params.rightMargin);
515 bottom = Math.max(bottom, params.mBottom + params.bottomMargin);
516 }
517 }
518 }
519 }
520
Romain Guyf7dabb02009-06-25 14:47:14 -0700521 if (isWrapContentWidth) {
Romain Guya1f3e4a2009-06-04 15:10:46 -0700522 // Width already has left padding in it since it was calculated by looking at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 // the right of each child view
524 width += mPaddingRight;
525
526 if (mLayoutParams.width >= 0) {
527 width = Math.max(width, mLayoutParams.width);
528 }
529
530 width = Math.max(width, getSuggestedMinimumWidth());
531 width = resolveSize(width, widthMeasureSpec);
Romain Guyf7dabb02009-06-25 14:47:14 -0700532
533 if (offsetHorizontalAxis) {
Romain Guyd10a5762009-07-28 11:18:14 -0700534 for (int i = 0; i < count; i++) {
Romain Guyf7dabb02009-06-25 14:47:14 -0700535 View child = getChildAt(i);
536 if (child.getVisibility() != GONE) {
537 LayoutParams params = (LayoutParams) child.getLayoutParams();
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700538 final int[] rules = params.getRules(layoutDirection);
Romain Guyf7dabb02009-06-25 14:47:14 -0700539 if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
540 centerHorizontal(child, params, width);
Romain Guy42460ac2010-01-11 16:46:33 -0800541 } else if (rules[ALIGN_PARENT_RIGHT] != 0) {
542 final int childWidth = child.getMeasuredWidth();
543 params.mLeft = width - mPaddingRight - childWidth;
544 params.mRight = params.mLeft + childWidth;
Romain Guyf7dabb02009-06-25 14:47:14 -0700545 }
546 }
547 }
548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 }
Romain Guyf7dabb02009-06-25 14:47:14 -0700550
551 if (isWrapContentHeight) {
Romain Guya1f3e4a2009-06-04 15:10:46 -0700552 // Height already has top padding in it since it was calculated by looking at
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 // the bottom of each child view
554 height += mPaddingBottom;
555
556 if (mLayoutParams.height >= 0) {
557 height = Math.max(height, mLayoutParams.height);
558 }
559
560 height = Math.max(height, getSuggestedMinimumHeight());
561 height = resolveSize(height, heightMeasureSpec);
Romain Guyf7dabb02009-06-25 14:47:14 -0700562
563 if (offsetVerticalAxis) {
564 for (int i = 0; i < count; i++) {
565 View child = getChildAt(i);
566 if (child.getVisibility() != GONE) {
567 LayoutParams params = (LayoutParams) child.getLayoutParams();
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700568 final int[] rules = params.getRules(layoutDirection);
Romain Guyf7dabb02009-06-25 14:47:14 -0700569 if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) {
570 centerVertical(child, params, height);
Romain Guy42460ac2010-01-11 16:46:33 -0800571 } else if (rules[ALIGN_PARENT_BOTTOM] != 0) {
572 final int childHeight = child.getMeasuredHeight();
573 params.mTop = height - mPaddingBottom - childHeight;
574 params.mBottom = params.mTop + childHeight;
Romain Guyf7dabb02009-06-25 14:47:14 -0700575 }
576 }
577 }
578 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580
581 if (horizontalGravity || verticalGravity) {
582 final Rect selfBounds = mSelfBounds;
583 selfBounds.set(mPaddingLeft, mPaddingTop, width - mPaddingRight,
584 height - mPaddingBottom);
585
586 final Rect contentBounds = mContentBounds;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700587 Gravity.apply(mGravity, right - left, bottom - top, selfBounds, contentBounds,
Fabrice Di Meglioc0053222011-06-13 12:16:51 -0700588 layoutDirection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589
590 final int horizontalOffset = contentBounds.left - left;
591 final int verticalOffset = contentBounds.top - top;
592 if (horizontalOffset != 0 || verticalOffset != 0) {
Romain Guy725015a2009-06-23 14:27:34 -0700593 for (int i = 0; i < count; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 View child = getChildAt(i);
595 if (child.getVisibility() != GONE && child != ignore) {
596 LayoutParams params = (LayoutParams) child.getLayoutParams();
Romain Guyd10a5762009-07-28 11:18:14 -0700597 if (horizontalGravity) {
598 params.mLeft += horizontalOffset;
599 params.mRight += horizontalOffset;
600 }
601 if (verticalGravity) {
602 params.mTop += verticalOffset;
603 params.mBottom += verticalOffset;
604 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606 }
607 }
608 }
609
Fabrice Di Meglio306fe5c2013-01-23 18:46:25 -0800610 if (isLayoutRtl()) {
611 final int offsetWidth = myWidth - width;
612 for (int i = 0; i < count; i++) {
613 View child = getChildAt(i);
614 if (child.getVisibility() != GONE) {
615 LayoutParams params = (LayoutParams) child.getLayoutParams();
616 params.mLeft -= offsetWidth;
617 params.mRight -= offsetWidth;
618 }
619 }
620
621 }
622
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 setMeasuredDimension(width, height);
624 }
625
626 private void alignBaseline(View child, LayoutParams params) {
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -0700627 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700628 int[] rules = params.getRules(layoutDirection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 int anchorBaseline = getRelatedViewBaseline(rules, ALIGN_BASELINE);
630
631 if (anchorBaseline != -1) {
632 LayoutParams anchorParams = getRelatedViewParams(rules, ALIGN_BASELINE);
633 if (anchorParams != null) {
634 int offset = anchorParams.mTop + anchorBaseline;
635 int baseline = child.getBaseline();
636 if (baseline != -1) {
637 offset -= baseline;
638 }
639 int height = params.mBottom - params.mTop;
640 params.mTop = offset;
641 params.mBottom = params.mTop + height;
642 }
643 }
644
645 if (mBaselineView == null) {
646 mBaselineView = child;
647 } else {
648 LayoutParams lp = (LayoutParams) mBaselineView.getLayoutParams();
649 if (params.mTop < lp.mTop || (params.mTop == lp.mTop && params.mLeft < lp.mLeft)) {
650 mBaselineView = child;
651 }
652 }
653 }
654
655 /**
656 * Measure a child. The child should have left, top, right and bottom information
657 * stored in its LayoutParams. If any of these values is -1 it means that the view
658 * can extend up to the corresponding edge.
659 *
660 * @param child Child to measure
661 * @param params LayoutParams associated with child
662 * @param myWidth Width of the the RelativeLayout
663 * @param myHeight Height of the RelativeLayout
664 */
Romain Guy725015a2009-06-23 14:27:34 -0700665 private void measureChild(View child, LayoutParams params, int myWidth, int myHeight) {
Romain Guye8fb03c2013-04-25 11:40:45 -0700666 int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft,
667 params.mRight, params.width,
668 params.leftMargin, params.rightMargin,
669 mPaddingLeft, mPaddingRight,
670 myWidth);
Romain Guy1b7c7912013-04-25 14:55:28 -0700671 int childHeightMeasureSpec = getChildMeasureSpec(params.mTop,
672 params.mBottom, params.height,
673 params.topMargin, params.bottomMargin,
674 mPaddingTop, mPaddingBottom,
675 myHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
677 }
678
Romain Guyf782e602009-06-25 15:26:49 -0700679 private void measureChildHorizontal(View child, LayoutParams params, int myWidth, int myHeight) {
Romain Guy95607032009-06-24 14:37:03 -0700680 int childWidthMeasureSpec = getChildMeasureSpec(params.mLeft,
681 params.mRight, params.width,
682 params.leftMargin, params.rightMargin,
683 mPaddingLeft, mPaddingRight,
684 myWidth);
Romain Guy76d59a32013-04-08 10:51:35 -0700685 int maxHeight = myHeight;
686 if (mMeasureVerticalWithPaddingMargin) {
687 maxHeight = Math.max(0, myHeight - mPaddingTop - mPaddingBottom -
688 params.topMargin - params.bottomMargin);
689 }
Romain Guyf782e602009-06-25 15:26:49 -0700690 int childHeightMeasureSpec;
Adam Powell7da4b732012-12-07 15:28:33 -0800691 if (myHeight < 0 && !mAllowBrokenMeasureSpecs) {
Romain Guyf16c7a92013-02-11 17:43:59 -0800692 if (params.height >= 0) {
693 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
694 params.height, MeasureSpec.EXACTLY);
695 } else {
696 // Negative values in a mySize/myWidth/myWidth value in RelativeLayout measurement
697 // is code for, "we got an unspecified mode in the RelativeLayout's measurespec."
698 // Carry it forward.
699 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
700 }
Adam Powell132a7422012-09-30 12:39:56 -0700701 } else if (params.width == LayoutParams.MATCH_PARENT) {
Romain Guy76d59a32013-04-08 10:51:35 -0700702 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.EXACTLY);
Romain Guyf782e602009-06-25 15:26:49 -0700703 } else {
Romain Guy76d59a32013-04-08 10:51:35 -0700704 childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
Romain Guyf782e602009-06-25 15:26:49 -0700705 }
Romain Guy725015a2009-06-23 14:27:34 -0700706 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
707 }
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 /**
710 * Get a measure spec that accounts for all of the constraints on this view.
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700711 * This includes size constraints imposed by the RelativeLayout as well as
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 * the View's desired dimension.
713 *
714 * @param childStart The left or top field of the child's layout params
715 * @param childEnd The right or bottom field of the child's layout params
716 * @param childSize The child's desired size (the width or height field of
717 * the child's layout params)
718 * @param startMargin The left or top margin
719 * @param endMargin The right or bottom margin
720 * @param startPadding mPaddingLeft or mPaddingTop
721 * @param endPadding mPaddingRight or mPaddingBottom
722 * @param mySize The width or height of this view (the RelativeLayout)
723 * @return MeasureSpec for the child
724 */
725 private int getChildMeasureSpec(int childStart, int childEnd,
726 int childSize, int startMargin, int endMargin, int startPadding,
727 int endPadding, int mySize) {
Adam Powell7da4b732012-12-07 15:28:33 -0800728 if (mySize < 0 && !mAllowBrokenMeasureSpecs) {
Romain Guyf16c7a92013-02-11 17:43:59 -0800729 if (childSize >= 0) {
730 return MeasureSpec.makeMeasureSpec(childSize, MeasureSpec.EXACTLY);
731 }
Adam Powell132a7422012-09-30 12:39:56 -0700732 // Negative values in a mySize/myWidth/myWidth value in RelativeLayout measurement
733 // is code for, "we got an unspecified mode in the RelativeLayout's measurespec."
734 // Carry it forward.
735 return MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
736 }
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 int childSpecMode = 0;
739 int childSpecSize = 0;
740
741 // Figure out start and end bounds.
742 int tempStart = childStart;
743 int tempEnd = childEnd;
744
745 // If the view did not express a layout constraint for an edge, use
746 // view's margins and our padding
747 if (tempStart < 0) {
748 tempStart = startPadding + startMargin;
749 }
750 if (tempEnd < 0) {
751 tempEnd = mySize - endPadding - endMargin;
752 }
753
754 // Figure out maximum size available to this view
755 int maxAvailable = tempEnd - tempStart;
756
757 if (childStart >= 0 && childEnd >= 0) {
758 // Constraints fixed both edges, so child must be an exact size
759 childSpecMode = MeasureSpec.EXACTLY;
760 childSpecSize = maxAvailable;
761 } else {
762 if (childSize >= 0) {
763 // Child wanted an exact size. Give as much as possible
764 childSpecMode = MeasureSpec.EXACTLY;
765
766 if (maxAvailable >= 0) {
767 // We have a maxmum size in this dimension.
768 childSpecSize = Math.min(maxAvailable, childSize);
769 } else {
770 // We can grow in this dimension.
771 childSpecSize = childSize;
772 }
Romain Guy980a9382010-01-08 15:06:28 -0800773 } else if (childSize == LayoutParams.MATCH_PARENT) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700774 // Child wanted to be as big as possible. Give all available
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 // space
776 childSpecMode = MeasureSpec.EXACTLY;
777 childSpecSize = maxAvailable;
778 } else if (childSize == LayoutParams.WRAP_CONTENT) {
779 // Child wants to wrap content. Use AT_MOST
780 // to communicate available space if we know
781 // our max size
782 if (maxAvailable >= 0) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700783 // We have a maximum size in this dimension.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 childSpecMode = MeasureSpec.AT_MOST;
785 childSpecSize = maxAvailable;
786 } else {
787 // We can grow in this dimension. Child can be as big as it
788 // wants
789 childSpecMode = MeasureSpec.UNSPECIFIED;
790 childSpecSize = 0;
791 }
792 }
793 }
794
795 return MeasureSpec.makeMeasureSpec(childSpecSize, childSpecMode);
796 }
797
Romain Guyf7dabb02009-06-25 14:47:14 -0700798 private boolean positionChildHorizontal(View child, LayoutParams params, int myWidth,
799 boolean wrapContent) {
800
Fabrice Di Meglioe56ffdc2012-09-23 14:51:16 -0700801 final int layoutDirection = getLayoutDirection();
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700802 int[] rules = params.getRules(layoutDirection);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803
804 if (params.mLeft < 0 && params.mRight >= 0) {
805 // Right is fixed, but left varies
806 params.mLeft = params.mRight - child.getMeasuredWidth();
807 } else if (params.mLeft >= 0 && params.mRight < 0) {
808 // Left is fixed, but right varies
809 params.mRight = params.mLeft + child.getMeasuredWidth();
810 } else if (params.mLeft < 0 && params.mRight < 0) {
811 // Both left and right vary
Romain Guyf7dabb02009-06-25 14:47:14 -0700812 if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_HORIZONTAL] != 0) {
813 if (!wrapContent) {
814 centerHorizontal(child, params, myWidth);
815 } else {
816 params.mLeft = mPaddingLeft + params.leftMargin;
817 params.mRight = params.mLeft + child.getMeasuredWidth();
818 }
819 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 } else {
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700821 // This is the default case. For RTL we start from the right and for LTR we start
822 // from the left. This will give LEFT/TOP for LTR and RIGHT/TOP for RTL.
823 if (isLayoutRtl()) {
824 params.mRight = myWidth - mPaddingRight- params.rightMargin;
825 params.mLeft = params.mRight - child.getMeasuredWidth();
826 } else {
827 params.mLeft = mPaddingLeft + params.leftMargin;
828 params.mRight = params.mLeft + child.getMeasuredWidth();
829 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 }
831 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -0700832 return rules[ALIGN_PARENT_END] != 0;
Romain Guy725015a2009-06-23 14:27:34 -0700833 }
834
Romain Guyf7dabb02009-06-25 14:47:14 -0700835 private boolean positionChildVertical(View child, LayoutParams params, int myHeight,
836 boolean wrapContent) {
837
Romain Guy725015a2009-06-23 14:27:34 -0700838 int[] rules = params.getRules();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839
840 if (params.mTop < 0 && params.mBottom >= 0) {
841 // Bottom is fixed, but top varies
842 params.mTop = params.mBottom - child.getMeasuredHeight();
843 } else if (params.mTop >= 0 && params.mBottom < 0) {
844 // Top is fixed, but bottom varies
845 params.mBottom = params.mTop + child.getMeasuredHeight();
846 } else if (params.mTop < 0 && params.mBottom < 0) {
847 // Both top and bottom vary
Romain Guyf7dabb02009-06-25 14:47:14 -0700848 if (rules[CENTER_IN_PARENT] != 0 || rules[CENTER_VERTICAL] != 0) {
849 if (!wrapContent) {
850 centerVertical(child, params, myHeight);
851 } else {
852 params.mTop = mPaddingTop + params.topMargin;
853 params.mBottom = params.mTop + child.getMeasuredHeight();
854 }
855 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 } else {
857 params.mTop = mPaddingTop + params.topMargin;
858 params.mBottom = params.mTop + child.getMeasuredHeight();
859 }
860 }
Romain Guy42460ac2010-01-11 16:46:33 -0800861 return rules[ALIGN_PARENT_BOTTOM] != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 }
863
Fabrice Di Meglio54726132013-01-18 18:36:45 -0800864 private void applyHorizontalSizeRules(LayoutParams childParams, int myWidth, int[] rules) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 RelativeLayout.LayoutParams anchorParams;
866
867 // -1 indicated a "soft requirement" in that direction. For example:
868 // left=10, right=-1 means the view must start at 10, but can go as far as it wants to the right
869 // left =-1, right=10 means the view must end at 10, but can go as far as it wants to the left
870 // left=10, right=20 means the left and right ends are both fixed
871 childParams.mLeft = -1;
872 childParams.mRight = -1;
873
874 anchorParams = getRelatedViewParams(rules, LEFT_OF);
875 if (anchorParams != null) {
876 childParams.mRight = anchorParams.mLeft - (anchorParams.leftMargin +
877 childParams.rightMargin);
878 } else if (childParams.alignWithParent && rules[LEFT_OF] != 0) {
879 if (myWidth >= 0) {
880 childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 }
882 }
883
884 anchorParams = getRelatedViewParams(rules, RIGHT_OF);
885 if (anchorParams != null) {
886 childParams.mLeft = anchorParams.mRight + (anchorParams.rightMargin +
887 childParams.leftMargin);
888 } else if (childParams.alignWithParent && rules[RIGHT_OF] != 0) {
889 childParams.mLeft = mPaddingLeft + childParams.leftMargin;
890 }
891
892 anchorParams = getRelatedViewParams(rules, ALIGN_LEFT);
893 if (anchorParams != null) {
894 childParams.mLeft = anchorParams.mLeft + childParams.leftMargin;
895 } else if (childParams.alignWithParent && rules[ALIGN_LEFT] != 0) {
896 childParams.mLeft = mPaddingLeft + childParams.leftMargin;
897 }
898
899 anchorParams = getRelatedViewParams(rules, ALIGN_RIGHT);
900 if (anchorParams != null) {
901 childParams.mRight = anchorParams.mRight - childParams.rightMargin;
902 } else if (childParams.alignWithParent && rules[ALIGN_RIGHT] != 0) {
903 if (myWidth >= 0) {
904 childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906 }
907
Romain Guy1b7c7912013-04-25 14:55:28 -0700908 if (0 != rules[ALIGN_PARENT_LEFT]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 childParams.mLeft = mPaddingLeft + childParams.leftMargin;
910 }
911
Romain Guy1b7c7912013-04-25 14:55:28 -0700912 if (0 != rules[ALIGN_PARENT_RIGHT]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 if (myWidth >= 0) {
914 childParams.mRight = myWidth - mPaddingRight - childParams.rightMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 }
916 }
Romain Guy725015a2009-06-23 14:27:34 -0700917 }
918
919 private void applyVerticalSizeRules(LayoutParams childParams, int myHeight) {
920 int[] rules = childParams.getRules();
921 RelativeLayout.LayoutParams anchorParams;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
923 childParams.mTop = -1;
924 childParams.mBottom = -1;
925
926 anchorParams = getRelatedViewParams(rules, ABOVE);
927 if (anchorParams != null) {
928 childParams.mBottom = anchorParams.mTop - (anchorParams.topMargin +
929 childParams.bottomMargin);
930 } else if (childParams.alignWithParent && rules[ABOVE] != 0) {
931 if (myHeight >= 0) {
932 childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934 }
935
936 anchorParams = getRelatedViewParams(rules, BELOW);
937 if (anchorParams != null) {
938 childParams.mTop = anchorParams.mBottom + (anchorParams.bottomMargin +
939 childParams.topMargin);
940 } else if (childParams.alignWithParent && rules[BELOW] != 0) {
941 childParams.mTop = mPaddingTop + childParams.topMargin;
942 }
943
944 anchorParams = getRelatedViewParams(rules, ALIGN_TOP);
945 if (anchorParams != null) {
946 childParams.mTop = anchorParams.mTop + childParams.topMargin;
947 } else if (childParams.alignWithParent && rules[ALIGN_TOP] != 0) {
948 childParams.mTop = mPaddingTop + childParams.topMargin;
949 }
950
951 anchorParams = getRelatedViewParams(rules, ALIGN_BOTTOM);
952 if (anchorParams != null) {
953 childParams.mBottom = anchorParams.mBottom - childParams.bottomMargin;
954 } else if (childParams.alignWithParent && rules[ALIGN_BOTTOM] != 0) {
955 if (myHeight >= 0) {
956 childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 }
958 }
959
Romain Guy1b7c7912013-04-25 14:55:28 -0700960 if (0 != rules[ALIGN_PARENT_TOP]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 childParams.mTop = mPaddingTop + childParams.topMargin;
962 }
963
Romain Guy1b7c7912013-04-25 14:55:28 -0700964 if (0 != rules[ALIGN_PARENT_BOTTOM]) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 if (myHeight >= 0) {
966 childParams.mBottom = myHeight - mPaddingBottom - childParams.bottomMargin;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968 }
969
970 if (rules[ALIGN_BASELINE] != 0) {
971 mHasBaselineAlignedChild = true;
972 }
973 }
974
975 private View getRelatedView(int[] rules, int relation) {
976 int id = rules[relation];
977 if (id != 0) {
Romain Guy1ab621e2009-06-25 13:31:57 -0700978 DependencyGraph.Node node = mGraph.mKeyNodes.get(id);
Romain Guya0fd1d72009-06-24 14:25:43 -0700979 if (node == null) return null;
980 View v = node.view;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981
982 // Find the first non-GONE view up the chain
983 while (v.getVisibility() == View.GONE) {
Fabrice Di Meglio13705ed2012-11-26 20:05:31 -0800984 rules = ((LayoutParams) v.getLayoutParams()).getRules(v.getLayoutDirection());
Romain Guy1ab621e2009-06-25 13:31:57 -0700985 node = mGraph.mKeyNodes.get((rules[relation]));
Romain Guya0fd1d72009-06-24 14:25:43 -0700986 if (node == null) return null;
987 v = node.view;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 }
989
990 return v;
991 }
992
993 return null;
994 }
995
996 private LayoutParams getRelatedViewParams(int[] rules, int relation) {
997 View v = getRelatedView(rules, relation);
998 if (v != null) {
999 ViewGroup.LayoutParams params = v.getLayoutParams();
1000 if (params instanceof LayoutParams) {
1001 return (LayoutParams) v.getLayoutParams();
1002 }
1003 }
1004 return null;
1005 }
1006
1007 private int getRelatedViewBaseline(int[] rules, int relation) {
1008 View v = getRelatedView(rules, relation);
1009 if (v != null) {
1010 return v.getBaseline();
1011 }
1012 return -1;
1013 }
1014
Romain Guyf16c7a92013-02-11 17:43:59 -08001015 private static void centerHorizontal(View child, LayoutParams params, int myWidth) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001016 int childWidth = child.getMeasuredWidth();
1017 int left = (myWidth - childWidth) / 2;
1018
1019 params.mLeft = left;
1020 params.mRight = left + childWidth;
1021 }
1022
Romain Guyf16c7a92013-02-11 17:43:59 -08001023 private static void centerVertical(View child, LayoutParams params, int myHeight) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 int childHeight = child.getMeasuredHeight();
1025 int top = (myHeight - childHeight) / 2;
1026
1027 params.mTop = top;
1028 params.mBottom = top + childHeight;
1029 }
1030
1031 @Override
1032 protected void onLayout(boolean changed, int l, int t, int r, int b) {
1033 // The layout has actually already been performed and the positions
1034 // cached. Apply the cached values to the children.
Fabrice Di Meglio13705ed2012-11-26 20:05:31 -08001035 final int count = getChildCount();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036
1037 for (int i = 0; i < count; i++) {
1038 View child = getChildAt(i);
1039 if (child.getVisibility() != GONE) {
1040 RelativeLayout.LayoutParams st =
1041 (RelativeLayout.LayoutParams) child.getLayoutParams();
1042 child.layout(st.mLeft, st.mTop, st.mRight, st.mBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001043 }
1044 }
1045 }
1046
1047 @Override
1048 public LayoutParams generateLayoutParams(AttributeSet attrs) {
1049 return new RelativeLayout.LayoutParams(getContext(), attrs);
1050 }
1051
1052 /**
1053 * Returns a set of layout parameters with a width of
1054 * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT},
1055 * a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} and no spanning.
1056 */
1057 @Override
1058 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
1059 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
1060 }
1061
1062 // Override to allow type-checking of LayoutParams.
1063 @Override
1064 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
1065 return p instanceof RelativeLayout.LayoutParams;
1066 }
1067
1068 @Override
1069 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
1070 return new LayoutParams(p);
1071 }
1072
svetoslavganov75986cf2009-05-14 22:28:01 -07001073 @Override
1074 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
1075 if (mTopToBottomLeftToRightSet == null) {
1076 mTopToBottomLeftToRightSet = new TreeSet<View>(new TopToBottomLeftToRightComparator());
1077 }
1078
1079 // sort children top-to-bottom and left-to-right
1080 for (int i = 0, count = getChildCount(); i < count; i++) {
1081 mTopToBottomLeftToRightSet.add(getChildAt(i));
1082 }
1083
1084 for (View view : mTopToBottomLeftToRightSet) {
Svetoslav Ganov0b0a41d2011-09-07 18:06:03 -07001085 if (view.getVisibility() == View.VISIBLE
1086 && view.dispatchPopulateAccessibilityEvent(event)) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001087 mTopToBottomLeftToRightSet.clear();
1088 return true;
1089 }
1090 }
1091
1092 mTopToBottomLeftToRightSet.clear();
1093 return false;
1094 }
1095
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -08001096 @Override
1097 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
1098 super.onInitializeAccessibilityEvent(event);
1099 event.setClassName(RelativeLayout.class.getName());
1100 }
1101
1102 @Override
1103 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
1104 super.onInitializeAccessibilityNodeInfo(info);
1105 info.setClassName(RelativeLayout.class.getName());
1106 }
1107
svetoslavganov75986cf2009-05-14 22:28:01 -07001108 /**
1109 * Compares two views in left-to-right and top-to-bottom fashion.
1110 */
1111 private class TopToBottomLeftToRightComparator implements Comparator<View> {
1112 public int compare(View first, View second) {
1113 // top - bottom
1114 int topDifference = first.getTop() - second.getTop();
1115 if (topDifference != 0) {
1116 return topDifference;
1117 }
1118 // left - right
1119 int leftDifference = first.getLeft() - second.getLeft();
1120 if (leftDifference != 0) {
1121 return leftDifference;
1122 }
1123 // break tie by height
1124 int heightDiference = first.getHeight() - second.getHeight();
1125 if (heightDiference != 0) {
1126 return heightDiference;
1127 }
1128 // break tie by width
1129 int widthDiference = first.getWidth() - second.getWidth();
1130 if (widthDiference != 0) {
1131 return widthDiference;
1132 }
1133 return 0;
1134 }
1135 }
1136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001137 /**
1138 * Per-child layout information associated with RelativeLayout.
1139 *
1140 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignWithParentIfMissing
1141 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toLeftOf
1142 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toRightOf
1143 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_above
1144 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_below
1145 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBaseline
1146 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignLeft
1147 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignTop
1148 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignRight
1149 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignBottom
1150 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentLeft
1151 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentTop
1152 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentRight
1153 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentBottom
1154 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerInParent
1155 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerHorizontal
1156 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_centerVertical
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001157 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toStartOf
1158 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_toEndOf
1159 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignStart
1160 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignEnd
1161 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentStart
1162 * @attr ref android.R.styleable#RelativeLayout_Layout_layout_alignParentEnd
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 */
1164 public static class LayoutParams extends ViewGroup.MarginLayoutParams {
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001165 @ViewDebug.ExportedProperty(category = "layout", resolveId = true, indexMapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001166 @ViewDebug.IntToString(from = ABOVE, to = "above"),
1167 @ViewDebug.IntToString(from = ALIGN_BASELINE, to = "alignBaseline"),
1168 @ViewDebug.IntToString(from = ALIGN_BOTTOM, to = "alignBottom"),
1169 @ViewDebug.IntToString(from = ALIGN_LEFT, to = "alignLeft"),
1170 @ViewDebug.IntToString(from = ALIGN_PARENT_BOTTOM, to = "alignParentBottom"),
1171 @ViewDebug.IntToString(from = ALIGN_PARENT_LEFT, to = "alignParentLeft"),
1172 @ViewDebug.IntToString(from = ALIGN_PARENT_RIGHT, to = "alignParentRight"),
1173 @ViewDebug.IntToString(from = ALIGN_PARENT_TOP, to = "alignParentTop"),
1174 @ViewDebug.IntToString(from = ALIGN_RIGHT, to = "alignRight"),
1175 @ViewDebug.IntToString(from = ALIGN_TOP, to = "alignTop"),
1176 @ViewDebug.IntToString(from = BELOW, to = "below"),
1177 @ViewDebug.IntToString(from = CENTER_HORIZONTAL, to = "centerHorizontal"),
1178 @ViewDebug.IntToString(from = CENTER_IN_PARENT, to = "center"),
1179 @ViewDebug.IntToString(from = CENTER_VERTICAL, to = "centerVertical"),
1180 @ViewDebug.IntToString(from = LEFT_OF, to = "leftOf"),
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001181 @ViewDebug.IntToString(from = RIGHT_OF, to = "rightOf"),
1182 @ViewDebug.IntToString(from = ALIGN_START, to = "alignStart"),
1183 @ViewDebug.IntToString(from = ALIGN_END, to = "alignEnd"),
1184 @ViewDebug.IntToString(from = ALIGN_PARENT_START, to = "alignParentStart"),
1185 @ViewDebug.IntToString(from = ALIGN_PARENT_END, to = "alignParentEnd"),
1186 @ViewDebug.IntToString(from = START_OF, to = "startOf"),
1187 @ViewDebug.IntToString(from = END_OF, to = "endOf")
The Android Open Source Project10592532009-03-18 17:39:46 -07001188 }, mapping = {
1189 @ViewDebug.IntToString(from = TRUE, to = "true"),
Romain Guya1f3e4a2009-06-04 15:10:46 -07001190 @ViewDebug.IntToString(from = 0, to = "false/NO_ID")
The Android Open Source Project10592532009-03-18 17:39:46 -07001191 })
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 private int[] mRules = new int[VERB_COUNT];
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001194 private int[] mInitialRules = new int[VERB_COUNT];
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001195
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 private int mLeft, mTop, mRight, mBottom;
1197
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07001198 private int mStart = DEFAULT_MARGIN_RELATIVE;
1199 private int mEnd = DEFAULT_MARGIN_RELATIVE;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001200
1201 private boolean mRulesChanged = false;
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -08001202 private boolean mIsRtlCompatibilityMode = false;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * When true, uses the parent as the anchor if the anchor doesn't exist or if
1206 * the anchor's visibility is GONE.
1207 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001208 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 public boolean alignWithParent;
1210
1211 public LayoutParams(Context c, AttributeSet attrs) {
1212 super(c, attrs);
1213
1214 TypedArray a = c.obtainStyledAttributes(attrs,
1215 com.android.internal.R.styleable.RelativeLayout_Layout);
1216
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -08001217 final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
1218 mIsRtlCompatibilityMode = (targetSdkVersion < JELLY_BEAN_MR1 ||
1219 !c.getApplicationInfo().hasRtlSupport());
1220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 final int[] rules = mRules;
Romain Guyf16c7a92013-02-11 17:43:59 -08001222 //noinspection MismatchedReadAndWriteOfArray
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001223 final int[] initialRules = mInitialRules;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224
1225 final int N = a.getIndexCount();
1226 for (int i = 0; i < N; i++) {
1227 int attr = a.getIndex(i);
1228 switch (attr) {
1229 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignWithParentIfMissing:
1230 alignWithParent = a.getBoolean(attr, false);
1231 break;
1232 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toLeftOf:
1233 rules[LEFT_OF] = a.getResourceId(attr, 0);
1234 break;
1235 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toRightOf:
1236 rules[RIGHT_OF] = a.getResourceId(attr, 0);
1237 break;
1238 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_above:
1239 rules[ABOVE] = a.getResourceId(attr, 0);
1240 break;
1241 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_below:
1242 rules[BELOW] = a.getResourceId(attr, 0);
1243 break;
1244 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBaseline:
1245 rules[ALIGN_BASELINE] = a.getResourceId(attr, 0);
1246 break;
1247 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignLeft:
1248 rules[ALIGN_LEFT] = a.getResourceId(attr, 0);
1249 break;
1250 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignTop:
1251 rules[ALIGN_TOP] = a.getResourceId(attr, 0);
1252 break;
1253 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignRight:
1254 rules[ALIGN_RIGHT] = a.getResourceId(attr, 0);
1255 break;
1256 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignBottom:
1257 rules[ALIGN_BOTTOM] = a.getResourceId(attr, 0);
1258 break;
1259 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentLeft:
1260 rules[ALIGN_PARENT_LEFT] = a.getBoolean(attr, false) ? TRUE : 0;
1261 break;
1262 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentTop:
1263 rules[ALIGN_PARENT_TOP] = a.getBoolean(attr, false) ? TRUE : 0;
1264 break;
1265 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentRight:
1266 rules[ALIGN_PARENT_RIGHT] = a.getBoolean(attr, false) ? TRUE : 0;
1267 break;
1268 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentBottom:
1269 rules[ALIGN_PARENT_BOTTOM] = a.getBoolean(attr, false) ? TRUE : 0;
1270 break;
1271 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerInParent:
1272 rules[CENTER_IN_PARENT] = a.getBoolean(attr, false) ? TRUE : 0;
1273 break;
1274 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerHorizontal:
1275 rules[CENTER_HORIZONTAL] = a.getBoolean(attr, false) ? TRUE : 0;
1276 break;
1277 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_centerVertical:
1278 rules[CENTER_VERTICAL] = a.getBoolean(attr, false) ? TRUE : 0;
1279 break;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001280 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toStartOf:
1281 rules[START_OF] = a.getResourceId(attr, 0);
1282 break;
1283 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_toEndOf:
1284 rules[END_OF] = a.getResourceId(attr, 0);
1285 break;
1286 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignStart:
1287 rules[ALIGN_START] = a.getResourceId(attr, 0);
1288 break;
1289 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignEnd:
1290 rules[ALIGN_END] = a.getResourceId(attr, 0);
1291 break;
1292 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentStart:
1293 rules[ALIGN_PARENT_START] = a.getBoolean(attr, false) ? TRUE : 0;
1294 break;
1295 case com.android.internal.R.styleable.RelativeLayout_Layout_layout_alignParentEnd:
1296 rules[ALIGN_PARENT_END] = a.getBoolean(attr, false) ? TRUE : 0;
1297 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001298 }
1299 }
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07001300 mRulesChanged = true;
Romain Guyf16c7a92013-02-11 17:43:59 -08001301 System.arraycopy(rules, LEFT_OF, initialRules, LEFT_OF, VERB_COUNT);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001303 a.recycle();
1304 }
1305
1306 public LayoutParams(int w, int h) {
1307 super(w, h);
1308 }
1309
1310 /**
1311 * {@inheritDoc}
1312 */
1313 public LayoutParams(ViewGroup.LayoutParams source) {
1314 super(source);
1315 }
1316
1317 /**
1318 * {@inheritDoc}
1319 */
1320 public LayoutParams(ViewGroup.MarginLayoutParams source) {
1321 super(source);
1322 }
1323
1324 @Override
1325 public String debug(String output) {
1326 return output + "ViewGroup.LayoutParams={ width=" + sizeToString(width) +
1327 ", height=" + sizeToString(height) + " }";
1328 }
1329
1330 /**
1331 * Adds a layout rule to be interpreted by the RelativeLayout. This
1332 * method should only be used for constraints that don't refer to another sibling
1333 * (e.g., CENTER_IN_PARENT) or take a boolean value ({@link RelativeLayout#TRUE}
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001334 * for true or 0 for false). To specify a verb that takes a subject, use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 * {@link #addRule(int, int)} instead.
1336 *
1337 * @param verb One of the verbs defined by
1338 * {@link android.widget.RelativeLayout RelativeLayout}, such as
1339 * ALIGN_WITH_PARENT_LEFT.
1340 * @see #addRule(int, int)
1341 */
1342 public void addRule(int verb) {
1343 mRules[verb] = TRUE;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001344 mInitialRules[verb] = TRUE;
1345 mRulesChanged = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346 }
1347
1348 /**
1349 * Adds a layout rule to be interpreted by the RelativeLayout. Use this for
1350 * verbs that take a target, such as a sibling (ALIGN_RIGHT) or a boolean
1351 * value (VISIBLE).
1352 *
1353 * @param verb One of the verbs defined by
1354 * {@link android.widget.RelativeLayout RelativeLayout}, such as
1355 * ALIGN_WITH_PARENT_LEFT.
1356 * @param anchor The id of another view to use as an anchor,
1357 * or a boolean value(represented as {@link RelativeLayout#TRUE})
1358 * for true or 0 for false). For verbs that don't refer to another sibling
1359 * (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.
1360 * @see #addRule(int)
1361 */
1362 public void addRule(int verb, int anchor) {
1363 mRules[verb] = anchor;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001364 mInitialRules[verb] = anchor;
1365 mRulesChanged = true;
1366 }
1367
Fabrice Di Meglioa4c10302012-07-26 12:27:56 -07001368 /**
1369 * Removes a layout rule to be interpreted by the RelativeLayout.
1370 *
1371 * @param verb One of the verbs defined by
1372 * {@link android.widget.RelativeLayout RelativeLayout}, such as
1373 * ALIGN_WITH_PARENT_LEFT.
1374 * @see #addRule(int)
1375 * @see #addRule(int, int)
1376 */
1377 public void removeRule(int verb) {
1378 mRules[verb] = 0;
1379 mInitialRules[verb] = 0;
1380 mRulesChanged = true;
1381 }
1382
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001383 private boolean hasRelativeRules() {
1384 return (mInitialRules[START_OF] != 0 || mInitialRules[END_OF] != 0 ||
1385 mInitialRules[ALIGN_START] != 0 || mInitialRules[ALIGN_END] != 0 ||
1386 mInitialRules[ALIGN_PARENT_START] != 0 || mInitialRules[ALIGN_PARENT_END] != 0);
1387 }
1388
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -08001389 // The way we are resolving rules depends on the layout direction and if we are pre JB MR1
1390 // or not.
1391 //
1392 // If we are pre JB MR1 (said as "RTL compatibility mode"), "left"/"right" rules are having
1393 // predominance over any "start/end" rules that could have been defined. A special case:
1394 // if no "left"/"right" rule has been defined and "start"/"end" rules are defined then we
1395 // resolve those "start"/"end" rules to "left"/"right" respectively.
1396 //
1397 // If we are JB MR1+, then "start"/"end" rules are having predominance over "left"/"right"
1398 // rules. If no "start"/"end" rule is defined then we use "left"/"right" rules.
1399 //
1400 // In all cases, the result of the resolution should clear the "start"/"end" rules to leave
1401 // only the "left"/"right" rules at the end.
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001402 private void resolveRules(int layoutDirection) {
1403 final boolean isLayoutRtl = (layoutDirection == View.LAYOUT_DIRECTION_RTL);
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -08001404
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001405 // Reset to initial state
Romain Guyf16c7a92013-02-11 17:43:59 -08001406 System.arraycopy(mInitialRules, LEFT_OF, mRules, LEFT_OF, VERB_COUNT);
Fabrice Di Meglioc44d8802013-02-19 17:42:13 -08001407
1408 // Apply rules depending on direction and if we are in RTL compatibility mode
1409 if (mIsRtlCompatibilityMode) {
1410 if (mRules[ALIGN_START] != 0) {
1411 if (mRules[ALIGN_LEFT] == 0) {
1412 // "left" rule is not defined but "start" rule is: use the "start" rule as
1413 // the "left" rule
1414 mRules[ALIGN_LEFT] = mRules[ALIGN_START];
1415 }
1416 mRules[ALIGN_START] = 0;
1417 }
1418
1419 if (mRules[ALIGN_END] != 0) {
1420 if (mRules[ALIGN_RIGHT] == 0) {
1421 // "right" rule is not defined but "end" rule is: use the "end" rule as the
1422 // "right" rule
1423 mRules[ALIGN_RIGHT] = mRules[ALIGN_END];
1424 }
1425 mRules[ALIGN_END] = 0;
1426 }
1427
1428 if (mRules[START_OF] != 0) {
1429 if (mRules[LEFT_OF] == 0) {
1430 // "left" rule is not defined but "start" rule is: use the "start" rule as
1431 // the "left" rule
1432 mRules[LEFT_OF] = mRules[START_OF];
1433 }
1434 mRules[START_OF] = 0;
1435 }
1436
1437 if (mRules[END_OF] != 0) {
1438 if (mRules[RIGHT_OF] == 0) {
1439 // "right" rule is not defined but "end" rule is: use the "end" rule as the
1440 // "right" rule
1441 mRules[RIGHT_OF] = mRules[END_OF];
1442 }
1443 mRules[END_OF] = 0;
1444 }
1445
1446 if (mRules[ALIGN_PARENT_START] != 0) {
1447 if (mRules[ALIGN_PARENT_LEFT] == 0) {
1448 // "left" rule is not defined but "start" rule is: use the "start" rule as
1449 // the "left" rule
1450 mRules[ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START];
1451 }
1452 mRules[ALIGN_PARENT_START] = 0;
1453 }
1454
1455 if (mRules[ALIGN_PARENT_RIGHT] == 0) {
1456 if (mRules[ALIGN_PARENT_RIGHT] == 0) {
1457 // "right" rule is not defined but "end" rule is: use the "end" rule as the
1458 // "right" rule
1459 mRules[ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END];
1460 }
1461 mRules[ALIGN_PARENT_END] = 0;
1462 }
1463 } else {
1464 // JB MR1+ case
1465 if ((mRules[ALIGN_START] != 0 || mRules[ALIGN_END] != 0) &&
1466 (mRules[ALIGN_LEFT] != 0 || mRules[ALIGN_RIGHT] != 0)) {
1467 // "start"/"end" rules take precedence over "left"/"right" rules
1468 mRules[ALIGN_LEFT] = 0;
1469 mRules[ALIGN_RIGHT] = 0;
1470 }
1471 if (mRules[ALIGN_START] != 0) {
1472 // "start" rule resolved to "left" or "right" depending on the direction
1473 mRules[isLayoutRtl ? ALIGN_RIGHT : ALIGN_LEFT] = mRules[ALIGN_START];
1474 mRules[ALIGN_START] = 0;
1475 }
1476 if (mRules[ALIGN_END] != 0) {
1477 // "end" rule resolved to "left" or "right" depending on the direction
1478 mRules[isLayoutRtl ? ALIGN_LEFT : ALIGN_RIGHT] = mRules[ALIGN_END];
1479 mRules[ALIGN_END] = 0;
1480 }
1481
1482 if ((mRules[START_OF] != 0 || mRules[END_OF] != 0) &&
1483 (mRules[LEFT_OF] != 0 || mRules[RIGHT_OF] != 0)) {
1484 // "start"/"end" rules take precedence over "left"/"right" rules
1485 mRules[LEFT_OF] = 0;
1486 mRules[RIGHT_OF] = 0;
1487 }
1488 if (mRules[START_OF] != 0) {
1489 // "start" rule resolved to "left" or "right" depending on the direction
1490 mRules[isLayoutRtl ? RIGHT_OF : LEFT_OF] = mRules[START_OF];
1491 mRules[START_OF] = 0;
1492 }
1493 if (mRules[END_OF] != 0) {
1494 // "end" rule resolved to "left" or "right" depending on the direction
1495 mRules[isLayoutRtl ? LEFT_OF : RIGHT_OF] = mRules[END_OF];
1496 mRules[END_OF] = 0;
1497 }
1498
1499 if ((mRules[ALIGN_PARENT_START] != 0 || mRules[ALIGN_PARENT_END] != 0) &&
1500 (mRules[ALIGN_PARENT_LEFT] != 0 || mRules[ALIGN_PARENT_RIGHT] != 0)) {
1501 // "start"/"end" rules take precedence over "left"/"right" rules
1502 mRules[ALIGN_PARENT_LEFT] = 0;
1503 mRules[ALIGN_PARENT_RIGHT] = 0;
1504 }
1505 if (mRules[ALIGN_PARENT_START] != 0) {
1506 // "start" rule resolved to "left" or "right" depending on the direction
1507 mRules[isLayoutRtl ? ALIGN_PARENT_RIGHT : ALIGN_PARENT_LEFT] = mRules[ALIGN_PARENT_START];
1508 mRules[ALIGN_PARENT_START] = 0;
1509 }
1510 if (mRules[ALIGN_PARENT_END] != 0) {
1511 // "end" rule resolved to "left" or "right" depending on the direction
1512 mRules[isLayoutRtl ? ALIGN_PARENT_LEFT : ALIGN_PARENT_RIGHT] = mRules[ALIGN_PARENT_END];
1513 mRules[ALIGN_PARENT_END] = 0;
1514 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001515 }
1516 mRulesChanged = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518
1519 /**
1520 * Retrieves a complete list of all supported rules, where the index is the rule
1521 * verb, and the element value is the value specified, or "false" if it was never
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001522 * set. If there are relative rules defined (*_START / *_END), they will be resolved
1523 * depending on the layout direction.
1524 *
1525 * @param layoutDirection the direction of the layout.
1526 * Should be either {@link View#LAYOUT_DIRECTION_LTR}
1527 * or {@link View#LAYOUT_DIRECTION_RTL}
1528 * @return the supported rules
1529 * @see #addRule(int, int)
1530 *
1531 * @hide
1532 */
1533 public int[] getRules(int layoutDirection) {
1534 if (hasRelativeRules() &&
1535 (mRulesChanged || layoutDirection != getLayoutDirection())) {
1536 resolveRules(layoutDirection);
1537 if (layoutDirection != getLayoutDirection()) {
1538 setLayoutDirection(layoutDirection);
1539 }
1540 }
1541 return mRules;
1542 }
1543
1544 /**
1545 * Retrieves a complete list of all supported rules, where the index is the rule
1546 * verb, and the element value is the value specified, or "false" if it was never
1547 * set. There will be no resolution of relative rules done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 *
1549 * @return the supported rules
1550 * @see #addRule(int, int)
1551 */
1552 public int[] getRules() {
1553 return mRules;
1554 }
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001555
1556 @Override
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07001557 public void resolveLayoutDirection(int layoutDirection) {
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001558 final boolean isLayoutRtl = isLayoutRtl();
1559 if (isLayoutRtl) {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07001560 if (mStart != DEFAULT_MARGIN_RELATIVE) mRight = mStart;
1561 if (mEnd != DEFAULT_MARGIN_RELATIVE) mLeft = mEnd;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001562 } else {
Fabrice Di Meglio0072f642013-03-26 15:50:24 -07001563 if (mStart != DEFAULT_MARGIN_RELATIVE) mLeft = mStart;
1564 if (mEnd != DEFAULT_MARGIN_RELATIVE) mRight = mEnd;
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001565 }
1566
1567 if (hasRelativeRules() && layoutDirection != getLayoutDirection()) {
1568 resolveRules(layoutDirection);
1569 }
1570 // This will set the layout direction
Fabrice Di Meglio2918ab62012-10-10 16:39:25 -07001571 super.resolveLayoutDirection(layoutDirection);
Fabrice Di Megliof443f982012-07-13 20:24:03 -07001572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
Romain Guy725015a2009-06-23 14:27:34 -07001574
1575 private static class DependencyGraph {
1576 /**
Romain Guy1ab621e2009-06-25 13:31:57 -07001577 * List of all views in the graph.
1578 */
1579 private ArrayList<Node> mNodes = new ArrayList<Node>();
1580
1581 /**
Romain Guy725015a2009-06-23 14:27:34 -07001582 * List of nodes in the graph. Each node is identified by its
1583 * view id (see View#getId()).
1584 */
Romain Guy1ab621e2009-06-25 13:31:57 -07001585 private SparseArray<Node> mKeyNodes = new SparseArray<Node>();
Romain Guy725015a2009-06-23 14:27:34 -07001586
1587 /**
1588 * Temporary data structure used to build the list of roots
1589 * for this graph.
1590 */
Romain Guybc5d8762012-01-06 16:40:49 -08001591 private ArrayDeque<Node> mRoots = new ArrayDeque<Node>();
Romain Guy725015a2009-06-23 14:27:34 -07001592
1593 /**
1594 * Clears the graph.
1595 */
1596 void clear() {
Romain Guy1ab621e2009-06-25 13:31:57 -07001597 final ArrayList<Node> nodes = mNodes;
Romain Guy725015a2009-06-23 14:27:34 -07001598 final int count = nodes.size();
1599
1600 for (int i = 0; i < count; i++) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001601 nodes.get(i).release();
Romain Guy725015a2009-06-23 14:27:34 -07001602 }
1603 nodes.clear();
1604
Romain Guy1ab621e2009-06-25 13:31:57 -07001605 mKeyNodes.clear();
Romain Guy725015a2009-06-23 14:27:34 -07001606 mRoots.clear();
1607 }
1608
1609 /**
1610 * Adds a view to the graph.
1611 *
1612 * @param view The view to be added as a node to the graph.
1613 */
1614 void add(View view) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001615 final int id = view.getId();
1616 final Node node = Node.acquire(view);
1617
1618 if (id != View.NO_ID) {
1619 mKeyNodes.put(id, node);
1620 }
1621
1622 mNodes.add(node);
Romain Guy725015a2009-06-23 14:27:34 -07001623 }
1624
1625 /**
1626 * Builds a sorted list of views. The sorting order depends on the dependencies
1627 * between the view. For instance, if view C needs view A to be processed first
1628 * and view A needs view B to be processed first, the dependency graph
1629 * is: B -> A -> C. The sorted array will contain views B, A and C in this order.
1630 *
1631 * @param sorted The sorted list of views. The length of this array must
1632 * be equal to getChildCount().
1633 * @param rules The list of rules to take into account.
1634 */
1635 void getSortedViews(View[] sorted, int... rules) {
Romain Guybc5d8762012-01-06 16:40:49 -08001636 final ArrayDeque<Node> roots = findRoots(rules);
Romain Guy725015a2009-06-23 14:27:34 -07001637 int index = 0;
1638
Romain Guybc5d8762012-01-06 16:40:49 -08001639 Node node;
1640 while ((node = roots.pollLast()) != null) {
Romain Guy725015a2009-06-23 14:27:34 -07001641 final View view = node.view;
1642 final int key = view.getId();
1643
1644 sorted[index++] = view;
1645
Romain Guy6876b4f2013-06-03 14:25:56 -07001646 final ArrayMap<Node, DependencyGraph> dependents = node.dependents;
1647 final int count = dependents.size();
1648 for (int i = 0; i < count; i++) {
1649 final Node dependent = dependents.keyAt(i);
Romain Guy725015a2009-06-23 14:27:34 -07001650 final SparseArray<Node> dependencies = dependent.dependencies;
1651
1652 dependencies.remove(key);
1653 if (dependencies.size() == 0) {
1654 roots.add(dependent);
1655 }
1656 }
1657 }
1658
1659 if (index < sorted.length) {
1660 throw new IllegalStateException("Circular dependencies cannot exist"
1661 + " in RelativeLayout");
1662 }
1663 }
1664
1665 /**
1666 * Finds the roots of the graph. A root is a node with no dependency and
1667 * with [0..n] dependents.
1668 *
1669 * @param rulesFilter The list of rules to consider when building the
1670 * dependencies
1671 *
1672 * @return A list of node, each being a root of the graph
1673 */
Romain Guybc5d8762012-01-06 16:40:49 -08001674 private ArrayDeque<Node> findRoots(int[] rulesFilter) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001675 final SparseArray<Node> keyNodes = mKeyNodes;
1676 final ArrayList<Node> nodes = mNodes;
Romain Guy725015a2009-06-23 14:27:34 -07001677 final int count = nodes.size();
1678
1679 // Find roots can be invoked several times, so make sure to clear
1680 // all dependents and dependencies before running the algorithm
1681 for (int i = 0; i < count; i++) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001682 final Node node = nodes.get(i);
Romain Guy725015a2009-06-23 14:27:34 -07001683 node.dependents.clear();
1684 node.dependencies.clear();
1685 }
1686
1687 // Builds up the dependents and dependencies for each node of the graph
1688 for (int i = 0; i < count; i++) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001689 final Node node = nodes.get(i);
Romain Guy725015a2009-06-23 14:27:34 -07001690
1691 final LayoutParams layoutParams = (LayoutParams) node.view.getLayoutParams();
1692 final int[] rules = layoutParams.mRules;
1693 final int rulesCount = rulesFilter.length;
1694
1695 // Look only the the rules passed in parameter, this way we build only the
1696 // dependencies for a specific set of rules
1697 for (int j = 0; j < rulesCount; j++) {
1698 final int rule = rules[rulesFilter[j]];
1699 if (rule > 0) {
1700 // The node this node depends on
Romain Guy1ab621e2009-06-25 13:31:57 -07001701 final Node dependency = keyNodes.get(rule);
Romain Guyda3003e2009-07-19 19:47:42 -07001702 // Skip unknowns and self dependencies
1703 if (dependency == null || dependency == node) {
Romain Guyb8f8de82009-06-25 12:03:56 -07001704 continue;
1705 }
Romain Guy725015a2009-06-23 14:27:34 -07001706 // Add the current node as a dependent
Romain Guybc5d8762012-01-06 16:40:49 -08001707 dependency.dependents.put(node, this);
Romain Guy725015a2009-06-23 14:27:34 -07001708 // Add a dependency to the current node
1709 node.dependencies.put(rule, dependency);
1710 }
1711 }
1712 }
1713
Romain Guybc5d8762012-01-06 16:40:49 -08001714 final ArrayDeque<Node> roots = mRoots;
Romain Guy725015a2009-06-23 14:27:34 -07001715 roots.clear();
1716
1717 // Finds all the roots in the graph: all nodes with no dependencies
1718 for (int i = 0; i < count; i++) {
Romain Guy1ab621e2009-06-25 13:31:57 -07001719 final Node node = nodes.get(i);
Romain Guybc5d8762012-01-06 16:40:49 -08001720 if (node.dependencies.size() == 0) roots.addLast(node);
Romain Guy725015a2009-06-23 14:27:34 -07001721 }
1722
1723 return roots;
1724 }
1725
1726 /**
Romain Guy725015a2009-06-23 14:27:34 -07001727 * A node in the dependency graph. A node is a view, its list of dependencies
1728 * and its list of dependents.
1729 *
1730 * A node with no dependent is considered a root of the graph.
1731 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08001732 static class Node {
Romain Guy725015a2009-06-23 14:27:34 -07001733 /**
1734 * The view representing this node in the layout.
1735 */
1736 View view;
1737
1738 /**
1739 * The list of dependents for this node; a dependent is a node
1740 * that needs this node to be processed first.
1741 */
Romain Guy6876b4f2013-06-03 14:25:56 -07001742 final ArrayMap<Node, DependencyGraph> dependents =
1743 new ArrayMap<Node, DependencyGraph>();
Romain Guy725015a2009-06-23 14:27:34 -07001744
1745 /**
1746 * The list of dependencies for this node.
1747 */
1748 final SparseArray<Node> dependencies = new SparseArray<Node>();
1749
1750 /*
1751 * START POOL IMPLEMENTATION
1752 */
Romain Guybaac4632009-06-29 14:28:29 -07001753 // The pool is static, so all nodes instances are shared across
1754 // activities, that's why we give it a rather high limit
1755 private static final int POOL_LIMIT = 100;
Romain Guy6876b4f2013-06-03 14:25:56 -07001756 private static final SynchronizedPool<Node> sPool =
1757 new SynchronizedPool<Node>(POOL_LIMIT);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001758
Romain Guy725015a2009-06-23 14:27:34 -07001759 static Node acquire(View view) {
Svetoslav Ganovabae2a12012-11-27 16:59:37 -08001760 Node node = sPool.acquire();
1761 if (node == null) {
1762 node = new Node();
1763 }
Romain Guy725015a2009-06-23 14:27:34 -07001764 node.view = view;
Romain Guy725015a2009-06-23 14:27:34 -07001765 return node;
1766 }
1767
1768 void release() {
1769 view = null;
1770 dependents.clear();
1771 dependencies.clear();
1772
1773 sPool.release(this);
1774 }
1775 /*
1776 * END POOL IMPLEMENTATION
1777 */
1778 }
1779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780}