2 * This is a modified version of a class from the Android Open Source Project.
3 * The original copyright and license information follows.
5 * Copyright (C) 2006 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package com.blahti.drag;
22 import android.content.Context;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.RemoteViews.RemoteView;
29 * A layout that lets you specify exact locations (x/y coordinates) of its
30 * children. Absolute layouts are less flexible and harder to maintain than
31 * other types of layouts without absolute positioning.
33 * <p><strong>XML attributes</strong></p> <p> See {@link
34 * android.R.styleable#ViewGroup ViewGroup Attributes}, {@link
35 * android.R.styleable#View View Attributes}</p>
37 * <p>Note: This class is a clone of AbsoluteLayout, which is now deprecated.
41 public class MyAbsoluteLayout extends ViewGroup {
42 public MyAbsoluteLayout(Context context) {
46 public MyAbsoluteLayout(Context context, AttributeSet attrs) {
47 super(context, attrs);
50 public MyAbsoluteLayout(Context context, AttributeSet attrs,
52 super(context, attrs, defStyle);
56 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
57 int count = getChildCount();
62 // Find out how big everyone wants to be
63 measureChildren(widthMeasureSpec, heightMeasureSpec);
65 // Find rightmost and bottom-most child
66 for (int i = 0; i < count; i++) {
67 View child = getChildAt(i);
68 if (child.getVisibility() != GONE) {
72 MyAbsoluteLayout.LayoutParams lp
73 = (MyAbsoluteLayout.LayoutParams) child.getLayoutParams();
75 childRight = lp.x + child.getMeasuredWidth();
76 childBottom = lp.y + child.getMeasuredHeight();
78 maxWidth = Math.max(maxWidth, childRight);
79 maxHeight = Math.max(maxHeight, childBottom);
83 // Account for padding too
84 maxWidth += getPaddingLeft () + getPaddingRight ();
85 maxHeight += getPaddingTop () + getPaddingBottom ();
87 maxWidth += mPaddingLeft + mPaddingRight;
88 maxHeight += mPaddingTop + mPaddingBottom;
91 // Check against minimum height and width
92 maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
93 maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
95 setMeasuredDimension(resolveSize(maxWidth, widthMeasureSpec),
96 resolveSize(maxHeight, heightMeasureSpec));
100 * Returns a set of layout parameters with a width of
101 * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT},
102 * a height of {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT}
103 * and with the coordinates (0, 0).
106 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
107 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
111 protected void onLayout(boolean changed, int l, int t,
113 int count = getChildCount();
115 int paddingL = getPaddingLeft ();
116 int paddingT = getPaddingTop ();
117 for (int i = 0; i < count; i++) {
118 View child = getChildAt(i);
119 if (child.getVisibility() != GONE) {
121 MyAbsoluteLayout.LayoutParams lp =
122 (MyAbsoluteLayout.LayoutParams) child.getLayoutParams();
124 int childLeft = paddingL + lp.x;
125 int childTop = paddingT + lp.y;
127 int childLeft = mPaddingLeft + lp.x;
128 int childTop = mPaddingTop + lp.y;
130 child.layout(childLeft, childTop,
131 childLeft + child.getMeasuredWidth(),
132 childTop + child.getMeasuredHeight());
139 public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
140 return new MyAbsoluteLayout.LayoutParams(getContext(), attrs);
143 // Override to allow type-checking of LayoutParams.
145 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
146 return p instanceof MyAbsoluteLayout.LayoutParams;
150 protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
151 return new LayoutParams(p);
155 * Per-child layout information associated with MyAbsoluteLayout.
157 * {@link android.R.styleable#MyAbsoluteLayout_Layout Absolute Layout Attributes}
158 * for a list of all child view attributes that this class supports.
160 public static class LayoutParams extends ViewGroup.LayoutParams {
162 * The horizontal, or X, location of the child within the view group.
166 * The vertical, or Y, location of the child within the view group.
171 * Creates a new set of layout parameters with the specified width,
172 * height and location.
174 * @param width the width, either {@link #MATCH_PARENT},
175 {@link #WRAP_CONTENT} or a fixed size in pixels
176 * @param height the height, either {@link #MATCH_PARENT},
177 {@link #WRAP_CONTENT} or a fixed size in pixels
178 * @param x the X location of the child
179 * @param y the Y location of the child
181 public LayoutParams(int width, int height, int x, int y) {
182 super(width, height);
188 * Creates a new set of layout parameters. The values are extracted from
189 * the supplied attributes set and context. The XML attributes mapped
190 * to this set of layout parameters are:
193 * <li><code>layout_x</code>: the X location of the child</li>
194 * <li><code>layout_y</code>: the Y location of the child</li>
195 * <li>All the XML attributes from
196 * {@link android.view.ViewGroup.LayoutParams}</li>
199 * @param c the application environment
200 * @param attrs the set of attributes from which to extract the layout
203 public LayoutParams(Context c, AttributeSet attrs) {
205 /* FIX THIS eventually. Without this, I don't think you can put x and y in layout xml files.
206 TypedArray a = c.obtainStyledAttributes(attrs,
207 com.android.internal.R.styleable.AbsoluteLayout_Layout);
208 x = a.getDimensionPixelOffset(
209 com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_x, 0);
210 y = a.getDimensionPixelOffset(
211 com.android.internal.R.styleable.AbsoluteLayout_Layout_layout_y, 0);
219 public LayoutParams(ViewGroup.LayoutParams source) {
223 public String debug(String output) {
224 return output + "Absolute.LayoutParams={width="
225 + sizeToString(width) + ", height=" + sizeToString(height)
226 + " x=" + x + " y=" + y + "}";
230 * Converts the specified size to a readable String.
232 * @param size the size to convert
233 * @return a String instance representing the supplied size
237 protected static String sizeToString(int size) {
238 if (size == WRAP_CONTENT) {
239 return "wrap-content";
241 if (size == MATCH_PARENT) {
242 return "match-parent";
244 return String.valueOf(size);