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; 21 import android.view.View.MeasureSpec; 22 import android.view.View; 23 import android.view.ViewGroup; 24 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 * The buttons will be bottom aligned in their view on layout. 39 * 40 * Invocation: onMeasure is called first by the framework to know our 41 * size. Then onLayout is invoked to layout the buttons. 42 */ 43 // TODO: Blindly layout the buttons w/o checking if we overrun the 44 // bottom-right corner. 45 public class ButtonGridLayout extends ViewGroup { 46 private final int COLUMNS = 3; 47 private final int ROWS = 4; 48 49 // Width and height of a button 50 private int mButtonWidth; 51 private int mButtonHeight; 52 53 // Width and height of a button + padding. 54 private int mWidthInc; 55 private int mHeightInc; 56 57 // Height of the dialpad. Used to align it at the bottom of the 58 // view. 59 private int mHeight; 60 ButtonGridLayout(Context context)61 public ButtonGridLayout(Context context) { 62 super(context); 63 } 64 ButtonGridLayout(Context context, AttributeSet attrs)65 public ButtonGridLayout(Context context, AttributeSet attrs) { 66 super(context, attrs); 67 } 68 ButtonGridLayout(Context context, AttributeSet attrs, int defStyle)69 public ButtonGridLayout(Context context, AttributeSet attrs, int defStyle) { 70 super(context, attrs, defStyle); 71 } 72 73 @Override onLayout(boolean changed, int l, int t, int r, int b)74 protected void onLayout(boolean changed, int l, int t, int r, int b) { 75 int i = 0; 76 // The last row is bottom aligned. 77 int y = (b - t) - mHeight + mPaddingTop; 78 for (int row = 0; row < ROWS; row++) { 79 int x = mPaddingLeft; 80 for (int col = 0; col < COLUMNS; col++) { 81 View child = getChildAt(i); 82 83 child.layout(x, y, x + mButtonWidth, y + mButtonHeight); 84 85 x += mWidthInc; 86 i++; 87 } 88 y += mHeightInc; 89 } 90 } 91 92 @Override onMeasure(int widthMeasureSpec, int heightMeasureSpec)93 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 94 // Measure the first child and get it's size 95 View child = getChildAt(0); 96 child.measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED); 97 98 // Make sure the other children are measured as well, to initialize 99 for (int i = 1; i < getChildCount(); i++) { 100 getChildAt(i).measure(MeasureSpec.UNSPECIFIED , MeasureSpec.UNSPECIFIED); 101 } 102 103 // Store these to be reused in onLayout. 104 mButtonWidth = child.getMeasuredWidth(); 105 mButtonHeight = child.getMeasuredHeight(); 106 mWidthInc = mButtonWidth + mPaddingLeft + mPaddingRight; 107 mHeightInc = mButtonHeight + mPaddingTop + mPaddingBottom; 108 mHeight = ROWS * mHeightInc; 109 110 final int width = resolveSize(COLUMNS * mWidthInc, widthMeasureSpec); 111 final int height = resolveSize(mHeight, heightMeasureSpec); 112 113 setMeasuredDimension(width, height); 114 } 115 } 116