blob: ae80be09a2d47e294b15ac370b21371beb28e2fc [file] [log] [blame]
Evan Millar76c67fc2009-08-07 09:12:49 -07001/*
2 * Copyright (C) 2009 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.graphics.Canvas;
21import android.graphics.Color;
22import android.graphics.Rect;
23import android.graphics.drawable.ColorDrawable;
24import android.graphics.drawable.Drawable;
25import android.util.AttributeSet;
26import android.view.MotionEvent;
27import android.view.View;
Evan Millar56d2caa2009-08-20 20:30:12 -070028import android.view.ViewParent;
Evan Millar76c67fc2009-08-07 09:12:49 -070029import android.widget.HorizontalScrollView;
30import android.widget.LinearLayout;
31
32/** Extension of LinearLayout that takes care of drawing bottom strips over the tab children. */
33public class TabStripView extends LinearLayout {
34
35 private Drawable mBottomLeftStrip;
36 private Drawable mBottomRightStrip;
37 private int mSelectedTabIndex;
38
39 public TabStripView(Context context) {
40 this(context, null);
41 }
42
43 public TabStripView(Context context, AttributeSet attrs) {
44 super(context, attrs);
45 init();
46 }
47
48 private void init() {
49 mBottomLeftStrip = mContext.getResources().getDrawable(
50 R.drawable.tab_bottom);
51 mBottomRightStrip = mContext.getResources().getDrawable(
52 R.drawable.tab_bottom);
53 }
54
55 public void setSelected(int index, boolean selected) {
56 mSelectedTabIndex = index;
57 getChildAt(index).setSelected(selected);
58 }
59
60 @Override
Evan Millar56d2caa2009-08-20 20:30:12 -070061 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
62 ViewParent parent = getParent();
63 if (parent instanceof HorizontalScrollView) {
64 setMinimumWidth(((HorizontalScrollView) getParent()).getMeasuredWidth());
65 }
66
67 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
68 }
69
70 @Override
Evan Millar76c67fc2009-08-07 09:12:49 -070071 public void childDrawableStateChanged(View child) {
72 if (child == getChildAt(mSelectedTabIndex)) {
73 // To make sure that the bottom strip is redrawn
74 invalidate();
75 }
76 super.childDrawableStateChanged(child);
77 }
78
79 @Override
80 public void dispatchDraw(Canvas canvas) {
81 super.dispatchDraw(canvas);
82
83 View selectedChild = getChildAt(mSelectedTabIndex);
84
85 mBottomRightStrip.setState(selectedChild.getDrawableState());
86 mBottomLeftStrip.setState(selectedChild.getDrawableState());
87
88 Rect selBounds = new Rect(); // Bounds of the selected tab indicator
89 selBounds.left = selectedChild.getLeft() - getScrollX();
90 selBounds.right = selectedChild.getRight() - getScrollX();
91 final int myHeight = getHeight();
92 mBottomLeftStrip.setBounds(
93 Math.min(0, selBounds.left
94 - mBottomLeftStrip.getIntrinsicWidth()),
95 myHeight - mBottomLeftStrip.getIntrinsicHeight(),
96 selBounds.left,
97 myHeight);
98 mBottomRightStrip.setBounds(
99 selBounds.right,
100 myHeight - mBottomRightStrip.getIntrinsicHeight(),
101 Math.max(getWidth(),
102 selBounds.right + mBottomRightStrip.getIntrinsicWidth()),
103 myHeight);
104
105 mBottomLeftStrip.draw(canvas);
106 mBottomRightStrip.draw(canvas);
107 }
108
109}