blob: b8936e3a33865f8e6b0ffef9d0ffe1400ec26146 [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;
Nicolas Catania53f94fc2009-09-28 23:03:33 -070021import android.view.View.MeasureSpec;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080022import android.view.View;
23import android.view.ViewGroup;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080024
Nicolas Catania53f94fc2009-09-28 23:03:33 -070025/**
26 * Create a 4x3 grid of dial buttons.
27 *
28 * It was easier and more efficient to do it this way than use
29 * standard layouts. It's perfectly fine (and actually encouraged) to
30 * use custom layouts rather than piling up standard layouts.
31 *
32 * The horizontal and vertical spacings between buttons are controlled
33 * by the amount of padding (attributes on the ButtonGridLayout element):
34 * - horizontal = left + right padding and
35 * - vertical = top + bottom padding.
36 *
37 * This class assumes that all the buttons have the same size.
38 *
39 * Invocation: onMeasure is called first by the framework to know our
40 * size. Then onLayout is invoked to layout the buttons.
41 */
42// TODO: Blindly layout the buttons w/o checking if we overrun the
43// bottom-right corner.
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080044public class ButtonGridLayout extends ViewGroup {
Nicolas Catania53f94fc2009-09-28 23:03:33 -070045 private final int COLUMNS = 3;
46 private final int ROWS = 4;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080047
Nicolas Catania53f94fc2009-09-28 23:03:33 -070048 // Width and height of a button
49 private int mButtonWidth;
50 private int mButtonHeight;
51
52 // Width and height of a button + padding.
53 private int mWidthInc;
54 private int mHeightInc;
Nicolas Catania6613abf2009-09-17 07:41:23 -070055
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080056 public ButtonGridLayout(Context context) {
57 super(context);
58 }
59
60 public ButtonGridLayout(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 }
63
64 public ButtonGridLayout(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
66 }
Nicolas Catania6613abf2009-09-17 07:41:23 -070067
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080068 @Override
69 protected void onLayout(boolean changed, int l, int t, int r, int b) {
Nicolas Catania53f94fc2009-09-28 23:03:33 -070070 int i = 0;
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080071 int y = mPaddingTop;
Nicolas Catania53f94fc2009-09-28 23:03:33 -070072 for (int row = 0; row < ROWS; row++) {
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080073 int x = mPaddingLeft;
Nicolas Catania53f94fc2009-09-28 23:03:33 -070074 for (int col = 0; col < COLUMNS; col++) {
75 View child = getChildAt(i);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080076
Nicolas Catania53f94fc2009-09-28 23:03:33 -070077 child.layout(x, y, x + mButtonWidth, y + mButtonHeight);
78
79 x += mWidthInc;
80 i++;
81 }
82 y += mHeightInc;
83 }
84 }
Nicolas Catania6613abf2009-09-17 07:41:23 -070085
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080086 @Override
87 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Nicolas Cataniabb6bf6b2009-09-29 20:28:01 -070088 // Measure the first child and get it's size
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080089 View child = getChildAt(0);
90 child.measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED);
Nicolas Catania53f94fc2009-09-28 23:03:33 -070091
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080092 // Make sure the other children are measured as well, to initialize
93 for (int i = 1; i < getChildCount(); i++) {
Nicolas Catania6613abf2009-09-17 07:41:23 -070094 getChildAt(i).measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -080095 }
Nicolas Catania6613abf2009-09-17 07:41:23 -070096
Nicolas Catania53f94fc2009-09-28 23:03:33 -070097 // Store these to be reused in onLayout.
98 mButtonWidth = child.getMeasuredWidth();
99 mButtonHeight = child.getMeasuredHeight();
100 mWidthInc = mButtonWidth + mPaddingLeft + mPaddingRight;
101 mHeightInc = mButtonHeight + mPaddingTop + mPaddingBottom;
Nicolas Catania6613abf2009-09-17 07:41:23 -0700102
Nicolas Catania53f94fc2009-09-28 23:03:33 -0700103 final int width = resolveSize(COLUMNS * mWidthInc, widthMeasureSpec);
104 final int height = resolveSize(ROWS * mHeightInc, heightMeasureSpec);
Nicolas Catania6613abf2009-09-17 07:41:23 -0700105
Nicolas Catania53f94fc2009-09-28 23:03:33 -0700106 setMeasuredDimension(width, height);
The Android Open Source Project7aa0e4c2009-03-03 19:32:21 -0800107 }
108
109}