blob: 69eed973b8cc15a6c0c4ac5fea8f52a530edcfd1 [file] [log] [blame]
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -08001/*
2 * Copyright (C) 2008 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 com.android.contacts;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23import android.view.View.MeasureSpec;
24
25public class ButtonGridLayout extends ViewGroup {
26
27 private final int mColumns = 3;
Nicolas Catania6613abf2009-09-17 07:41:23 -070028
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080029 public ButtonGridLayout(Context context) {
30 super(context);
31 }
32
33 public ButtonGridLayout(Context context, AttributeSet attrs) {
34 super(context, attrs);
35 }
36
37 public ButtonGridLayout(Context context, AttributeSet attrs, int defStyle) {
38 super(context, attrs, defStyle);
39 }
Nicolas Catania6613abf2009-09-17 07:41:23 -070040
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080041 @Override
42 protected void onLayout(boolean changed, int l, int t, int r, int b) {
43 int y = mPaddingTop;
44 final int rows = getRows();
45 final View child0 = getChildAt(0);
46 final int yInc = (getHeight() - mPaddingTop - mPaddingBottom) / rows;
47 final int xInc = (getWidth() - mPaddingLeft - mPaddingRight) / mColumns;
48 final int childWidth = child0.getMeasuredWidth();
49 final int childHeight = child0.getMeasuredHeight();
50 final int xOffset = (xInc - childWidth) / 2;
51 final int yOffset = (yInc - childHeight) / 2;
Nicolas Catania6613abf2009-09-17 07:41:23 -070052
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080053 for (int row = 0; row < rows; row++) {
54 int x = mPaddingLeft;
55 for (int col = 0; col < mColumns; col++) {
56 int cell = row * mColumns + col;
57 if (cell >= getChildCount()) {
58 break;
59 }
60 View child = getChildAt(cell);
Nicolas Catania6613abf2009-09-17 07:41:23 -070061 child.layout(x + xOffset, y + yOffset,
62 x + xOffset + childWidth,
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080063 y + yOffset + childHeight);
64 x += xInc;
65 }
66 y += yInc;
67 }
68 }
69
70 private int getRows() {
Nicolas Catania6613abf2009-09-17 07:41:23 -070071 return (getChildCount() + mColumns - 1) / mColumns;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080072 }
Nicolas Catania6613abf2009-09-17 07:41:23 -070073
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080074 @Override
75 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
76 int width = mPaddingLeft + mPaddingRight;
77 int height = mPaddingTop + mPaddingBottom;
Nicolas Catania6613abf2009-09-17 07:41:23 -070078
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080079 // Measure the first child and get it's size
80 View child = getChildAt(0);
81 child.measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED);
82 int childWidth = child.getMeasuredWidth();
83 int childHeight = child.getMeasuredHeight();
84 // Make sure the other children are measured as well, to initialize
85 for (int i = 1; i < getChildCount(); i++) {
Nicolas Catania6613abf2009-09-17 07:41:23 -070086 getChildAt(i).measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080087 }
88 // All cells are going to be the size of the first child
89 width += mColumns * childWidth;
Nicolas Catania6613abf2009-09-17 07:41:23 -070090 final int finalWidth = resolveSize(width, widthMeasureSpec);
91
92 // The vertical padding between button must be the same as the
93 // horizontal one. The cumulative horizontal padding is the
94 // difference between 'width' and 'finalWidth'.
95 final int padding = (finalWidth - width) / mColumns;
96
97 height += getRows() * (childHeight + padding);
98
99 final int finalHeight = resolveSize(height, heightMeasureSpec);
100 setMeasuredDimension(finalWidth, finalHeight);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800101 }
102
103}