The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 21 | import android.view.View.MeasureSpec; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 24 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 25 | /** |
| 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 Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 44 | public class ButtonGridLayout extends ViewGroup { |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 45 | private final int COLUMNS = 3; |
| 46 | private final int ROWS = 4; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 47 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 48 | // 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 Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 55 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 56 | 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 Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 67 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 68 | @Override |
| 69 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 70 | int i = 0; |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 71 | int y = mPaddingTop; |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 72 | for (int row = 0; row < ROWS; row++) { |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 73 | int x = mPaddingLeft; |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 74 | for (int col = 0; col < COLUMNS; col++) { |
| 75 | View child = getChildAt(i); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 76 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 77 | child.layout(x, y, x + mButtonWidth, y + mButtonHeight); |
| 78 | |
| 79 | x += mWidthInc; |
| 80 | i++; |
| 81 | } |
| 82 | y += mHeightInc; |
| 83 | } |
| 84 | } |
Nicolas Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 85 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 86 | @Override |
| 87 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
Nicolas Catania | bb6bf6b | 2009-09-29 20:28:01 -0700 | [diff] [blame] | 88 | // Measure the first child and get it's size |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 89 | View child = getChildAt(0); |
| 90 | child.measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED); |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 91 | |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 92 | // Make sure the other children are measured as well, to initialize |
| 93 | for (int i = 1; i < getChildCount(); i++) { |
Nicolas Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 94 | getChildAt(i).measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 95 | } |
Nicolas Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 96 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 97 | // 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 Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 102 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 103 | final int width = resolveSize(COLUMNS * mWidthInc, widthMeasureSpec); |
| 104 | final int height = resolveSize(ROWS * mHeightInc, heightMeasureSpec); |
Nicolas Catania | 6613abf | 2009-09-17 07:41:23 -0700 | [diff] [blame] | 105 | |
Nicolas Catania | 53f94fc | 2009-09-28 23:03:33 -0700 | [diff] [blame] | 106 | setMeasuredDimension(width, height); |
The Android Open Source Project | 7aa0e4c | 2009-03-03 19:32:21 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } |