blob: 9496e6295dfc8cd002a7c6a38407a6c21596cc28 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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 android.widget;
18
Romain Guy61c9d4b2010-03-01 14:12:10 -080019import android.R;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.res.TypedArray;
23import android.graphics.Canvas;
24import android.graphics.Rect;
25import android.graphics.drawable.Drawable;
Mike Cleron76097642009-09-25 17:53:56 -070026import android.os.Build;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.view.View.OnFocusChangeListener;
Gilles Debunne97dfd342010-10-20 16:31:15 -070030import android.view.ViewGroup;
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -070031import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/**
Evan Millar3730bb12009-08-21 13:58:41 -070034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035 * Displays a list of tab labels representing each page in the parent's tab
36 * collection. The container object for this widget is
37 * {@link android.widget.TabHost TabHost}. When the user selects a tab, this
38 * object sends a message to the parent container, TabHost, to tell it to switch
39 * the displayed page. You typically won't use many methods directly on this
40 * object. The container TabHost is used to add labels, add the callback
41 * handler, and manage callbacks. You might call this object to iterate the list
42 * of tabs, or to tweak the layout of the tab list, but most methods should be
43 * called on the containing TabHost object.
Romain Guy7883c972010-03-01 16:39:17 -080044 *
45 * @attr ref android.R.styleable#TabWidget_divider
Romain Guy6b1e6962010-03-29 14:38:41 -070046 * @attr ref android.R.styleable#TabWidget_tabStripEnabled
47 * @attr ref android.R.styleable#TabWidget_tabStripLeft
48 * @attr ref android.R.styleable#TabWidget_tabStripRight
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 */
50public class TabWidget extends LinearLayout implements OnFocusChangeListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 private OnTabSelectionChanged mSelectionChangedListener;
Romain Guy61c9d4b2010-03-01 14:12:10 -080052
Gilles Debunne97dfd342010-10-20 16:31:15 -070053 // This value will be set to 0 as soon as the first tab is added to TabHost.
54 private int mSelectedTab = -1;
Romain Guy61c9d4b2010-03-01 14:12:10 -080055
Romain Guy65fe2c082010-03-29 12:27:30 -070056 private Drawable mLeftStrip;
57 private Drawable mRightStrip;
Romain Guy61c9d4b2010-03-01 14:12:10 -080058
Jack Veenstra53175142009-06-01 21:27:01 -070059 private boolean mDrawBottomStrips = true;
Romain Guy61c9d4b2010-03-01 14:12:10 -080060 private boolean mStripMoved;
61
Romain Guy61c9d4b2010-03-01 14:12:10 -080062 private final Rect mBounds = new Rect();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
Gilles Debunnec4d3f752011-01-25 20:11:45 -080064 // When positive, the widths and heights of tabs will be imposed so that they fit in parent
65 private int mImposedTabsHeight = -1;
66 private int[] mImposedTabWidths;
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 public TabWidget(Context context) {
69 this(context, null);
70 }
71
72 public TabWidget(Context context, AttributeSet attrs) {
73 this(context, attrs, com.android.internal.R.attr.tabWidgetStyle);
74 }
75
Alan Viverette617feb92013-09-09 18:09:13 -070076 public TabWidget(Context context, AttributeSet attrs, int defStyleAttr) {
77 this(context, attrs, defStyleAttr, 0);
78 }
79
80 public TabWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
81 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
Jeff Sharkey11f4a482011-08-08 21:05:40 -070083 final TypedArray a = context.obtainStyledAttributes(
Alan Viverette617feb92013-09-09 18:09:13 -070084 attrs, com.android.internal.R.styleable.TabWidget, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
Jeff Sharkey11f4a482011-08-08 21:05:40 -070086 setStripEnabled(a.getBoolean(R.styleable.TabWidget_tabStripEnabled, true));
87 setLeftStripDrawable(a.getDrawable(R.styleable.TabWidget_tabStripLeft));
88 setRightStripDrawable(a.getDrawable(R.styleable.TabWidget_tabStripRight));
Romain Guy61c9d4b2010-03-01 14:12:10 -080089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 a.recycle();
Romain Guy61c9d4b2010-03-01 14:12:10 -080091
92 initTabWidget();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 }
Evan Millar3730bb12009-08-21 13:58:41 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 @Override
96 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
97 mStripMoved = true;
98 super.onSizeChanged(w, h, oldw, oldh);
99 }
100
Evan Millar3730bb12009-08-21 13:58:41 -0700101 @Override
102 protected int getChildDrawingOrder(int childCount, int i) {
Gilles Debunne97dfd342010-10-20 16:31:15 -0700103 if (mSelectedTab == -1) {
Evan Millar3730bb12009-08-21 13:58:41 -0700104 return i;
Gilles Debunne97dfd342010-10-20 16:31:15 -0700105 } else {
106 // Always draw the selected tab last, so that drop shadows are drawn
107 // in the correct z-order.
108 if (i == childCount - 1) {
109 return mSelectedTab;
110 } else if (i >= mSelectedTab) {
111 return i + 1;
112 } else {
113 return i;
114 }
Evan Millar3730bb12009-08-21 13:58:41 -0700115 }
116 }
117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private void initTabWidget() {
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700119 setChildrenDrawingOrderEnabled(true);
Evan Millar3730bb12009-08-21 13:58:41 -0700120
Mike Cleron76097642009-09-25 17:53:56 -0700121 final Context context = mContext;
Gilles Debunne44c14732010-10-19 11:56:59 -0700122
123 // Tests the target Sdk version, as set in the Manifest. Could not be set using styles.xml
124 // in a values-v? directory which targets the current platform Sdk version instead.
Mike Cleron76097642009-09-25 17:53:56 -0700125 if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT) {
126 // Donut apps get old color scheme
Romain Guy65fe2c082010-03-29 12:27:30 -0700127 if (mLeftStrip == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800128 mLeftStrip = context.getDrawable(
Romain Guy61c9d4b2010-03-01 14:12:10 -0800129 com.android.internal.R.drawable.tab_bottom_left_v4);
130 }
Romain Guy65fe2c082010-03-29 12:27:30 -0700131 if (mRightStrip == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800132 mRightStrip = context.getDrawable(
Romain Guy61c9d4b2010-03-01 14:12:10 -0800133 com.android.internal.R.drawable.tab_bottom_right_v4);
134 }
Dianne Hackborn1ec7e202011-01-28 14:11:17 -0800135 } else {
136 // Use modern color scheme for Eclair and beyond
137 if (mLeftStrip == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800138 mLeftStrip = context.getDrawable(
Dianne Hackborn1ec7e202011-01-28 14:11:17 -0800139 com.android.internal.R.drawable.tab_bottom_left);
140 }
141 if (mRightStrip == null) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800142 mRightStrip = context.getDrawable(
Dianne Hackborn1ec7e202011-01-28 14:11:17 -0800143 com.android.internal.R.drawable.tab_bottom_right);
144 }
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700145 }
Mike Cleron76097642009-09-25 17:53:56 -0700146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 // Deal with focus, as we don't want the focus to go by default
148 // to a tab other than the current tab
149 setFocusable(true);
150 setOnFocusChangeListener(this);
151 }
152
Gilles Debunne6741c942011-01-25 20:11:45 -0800153 @Override
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800154 void measureChildBeforeLayout(View child, int childIndex,
155 int widthMeasureSpec, int totalWidth,
156 int heightMeasureSpec, int totalHeight) {
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700157 if (!isMeasureWithLargestChildEnabled() && mImposedTabsHeight >= 0) {
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800158 widthMeasureSpec = MeasureSpec.makeMeasureSpec(
159 totalWidth + mImposedTabWidths[childIndex], MeasureSpec.EXACTLY);
160 heightMeasureSpec = MeasureSpec.makeMeasureSpec(mImposedTabsHeight,
161 MeasureSpec.EXACTLY);
Gilles Debunne6741c942011-01-25 20:11:45 -0800162 }
163
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800164 super.measureChildBeforeLayout(child, childIndex,
165 widthMeasureSpec, totalWidth, heightMeasureSpec, totalHeight);
166 }
167
168 @Override
169 void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) {
Gilles Debunne52a5e6582011-02-28 17:58:35 -0800170 if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
171 super.measureHorizontal(widthMeasureSpec, heightMeasureSpec);
172 return;
173 }
174
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800175 // First, measure with no constraint
176 final int unspecifiedWidth = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800177 mImposedTabsHeight = -1;
Gilles Debunnecd59feb2011-02-25 14:34:20 -0800178 super.measureHorizontal(unspecifiedWidth, heightMeasureSpec);
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800179
180 int extraWidth = getMeasuredWidth() - MeasureSpec.getSize(widthMeasureSpec);
181 if (extraWidth > 0) {
182 final int count = getChildCount();
183
184 int childCount = 0;
Gilles Debunne6741c942011-01-25 20:11:45 -0800185 for (int i = 0; i < count; i++) {
186 final View child = getChildAt(i);
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800187 if (child.getVisibility() == GONE) continue;
188 childCount++;
Gilles Debunne6741c942011-01-25 20:11:45 -0800189 }
Gilles Debunnec4d3f752011-01-25 20:11:45 -0800190
191 if (childCount > 0) {
192 if (mImposedTabWidths == null || mImposedTabWidths.length != count) {
193 mImposedTabWidths = new int[count];
194 }
195 for (int i = 0; i < count; i++) {
196 final View child = getChildAt(i);
197 if (child.getVisibility() == GONE) continue;
198 final int childWidth = child.getMeasuredWidth();
199 final int delta = extraWidth / childCount;
200 final int newWidth = Math.max(0, childWidth - delta);
201 mImposedTabWidths[i] = newWidth;
202 // Make sure the extra width is evenly distributed, no int division remainder
203 extraWidth -= childWidth - newWidth; // delta may have been clamped
204 childCount--;
205 mImposedTabsHeight = Math.max(mImposedTabsHeight, child.getMeasuredHeight());
206 }
207 }
208 }
209
210 // Measure again, this time with imposed tab widths and respecting initial spec request
Gilles Debunne52a5e6582011-02-28 17:58:35 -0800211 super.measureHorizontal(widthMeasureSpec, heightMeasureSpec);
Gilles Debunne6741c942011-01-25 20:11:45 -0800212 }
213
214 /**
Jack Veenstra53175142009-06-01 21:27:01 -0700215 * Returns the tab indicator view at the given index.
216 *
217 * @param index the zero-based index of the tab indicator view to return
218 * @return the tab indicator view at the given index
219 */
220 public View getChildTabViewAt(int index) {
Jack Veenstra53175142009-06-01 21:27:01 -0700221 return getChildAt(index);
222 }
223
224 /**
225 * Returns the number of tab indicator views.
226 * @return the number of tab indicator views.
227 */
228 public int getTabCount() {
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700229 return getChildCount();
Jack Veenstra53175142009-06-01 21:27:01 -0700230 }
231
232 /**
233 * Sets the drawable to use as a divider between the tab indicators.
234 * @param drawable the divider drawable
235 */
Gilles Debunne6741c942011-01-25 20:11:45 -0800236 @Override
Jack Veenstra53175142009-06-01 21:27:01 -0700237 public void setDividerDrawable(Drawable drawable) {
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700238 super.setDividerDrawable(drawable);
Jack Veenstra53175142009-06-01 21:27:01 -0700239 }
240
241 /**
242 * Sets the drawable to use as a divider between the tab indicators.
243 * @param resId the resource identifier of the drawable to use as a
244 * divider.
245 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700246 public void setDividerDrawable(@DrawableRes int resId) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800247 setDividerDrawable(mContext.getDrawable(resId));
Jack Veenstra53175142009-06-01 21:27:01 -0700248 }
Romain Guy61c9d4b2010-03-01 14:12:10 -0800249
250 /**
251 * Sets the drawable to use as the left part of the strip below the
252 * tab indicators.
253 * @param drawable the left strip drawable
254 */
255 public void setLeftStripDrawable(Drawable drawable) {
Romain Guy65fe2c082010-03-29 12:27:30 -0700256 mLeftStrip = drawable;
Romain Guy42c79882010-03-01 17:20:57 -0800257 requestLayout();
258 invalidate();
Romain Guy61c9d4b2010-03-01 14:12:10 -0800259 }
Jack Veenstra53175142009-06-01 21:27:01 -0700260
261 /**
Romain Guy61c9d4b2010-03-01 14:12:10 -0800262 * Sets the drawable to use as the left part of the strip below the
263 * tab indicators.
264 * @param resId the resource identifier of the drawable to use as the
265 * left strip drawable
266 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700267 public void setLeftStripDrawable(@DrawableRes int resId) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800268 setLeftStripDrawable(mContext.getDrawable(resId));
Romain Guy61c9d4b2010-03-01 14:12:10 -0800269 }
270
271 /**
Marco Nelissen189f65c2010-03-22 10:59:00 -0700272 * Sets the drawable to use as the right part of the strip below the
Romain Guy61c9d4b2010-03-01 14:12:10 -0800273 * tab indicators.
Marco Nelissen189f65c2010-03-22 10:59:00 -0700274 * @param drawable the right strip drawable
Romain Guy61c9d4b2010-03-01 14:12:10 -0800275 */
276 public void setRightStripDrawable(Drawable drawable) {
Romain Guy65fe2c082010-03-29 12:27:30 -0700277 mRightStrip = drawable;
Romain Guy42c79882010-03-01 17:20:57 -0800278 requestLayout();
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700279 invalidate();
280 }
Romain Guy61c9d4b2010-03-01 14:12:10 -0800281
282 /**
Marco Nelissen189f65c2010-03-22 10:59:00 -0700283 * Sets the drawable to use as the right part of the strip below the
Romain Guy61c9d4b2010-03-01 14:12:10 -0800284 * tab indicators.
285 * @param resId the resource identifier of the drawable to use as the
Marco Nelissen189f65c2010-03-22 10:59:00 -0700286 * right strip drawable
Romain Guy61c9d4b2010-03-01 14:12:10 -0800287 */
Tor Norbye7b9c9122013-05-30 16:48:33 -0700288 public void setRightStripDrawable(@DrawableRes int resId) {
Alan Viverette8eea3ea2014-02-03 18:40:20 -0800289 setRightStripDrawable(mContext.getDrawable(resId));
Romain Guy61c9d4b2010-03-01 14:12:10 -0800290 }
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700291
Romain Guy61c9d4b2010-03-01 14:12:10 -0800292 /**
Jack Veenstra53175142009-06-01 21:27:01 -0700293 * Controls whether the bottom strips on the tab indicators are drawn or
294 * not. The default is to draw them. If the user specifies a custom
295 * view for the tab indicators, then the TabHost class calls this method
296 * to disable drawing of the bottom strips.
Romain Guy61c9d4b2010-03-01 14:12:10 -0800297 * @param stripEnabled true if the bottom strips should be drawn.
Jack Veenstra53175142009-06-01 21:27:01 -0700298 */
Romain Guy61c9d4b2010-03-01 14:12:10 -0800299 public void setStripEnabled(boolean stripEnabled) {
300 mDrawBottomStrips = stripEnabled;
Romain Guy42c79882010-03-01 17:20:57 -0800301 invalidate();
Romain Guy61c9d4b2010-03-01 14:12:10 -0800302 }
303
304 /**
305 * Indicates whether the bottom strips on the tab indicators are drawn
306 * or not.
307 */
308 public boolean isStripEnabled() {
309 return mDrawBottomStrips;
Jack Veenstra53175142009-06-01 21:27:01 -0700310 }
311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 @Override
313 public void childDrawableStateChanged(View child) {
Bjorn Bringertacdef592009-12-10 15:58:49 +0000314 if (getTabCount() > 0 && child == getChildTabViewAt(mSelectedTab)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 // To make sure that the bottom strip is redrawn
316 invalidate();
317 }
318 super.childDrawableStateChanged(child);
319 }
Evan Millar3730bb12009-08-21 13:58:41 -0700320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 @Override
322 public void dispatchDraw(Canvas canvas) {
323 super.dispatchDraw(canvas);
324
Bjorn Bringertacdef592009-12-10 15:58:49 +0000325 // Do nothing if there are no tabs.
326 if (getTabCount() == 0) return;
327
Jack Veenstra53175142009-06-01 21:27:01 -0700328 // If the user specified a custom view for the tab indicators, then
329 // do not draw the bottom strips.
330 if (!mDrawBottomStrips) {
331 // Skip drawing the bottom strips.
332 return;
333 }
334
Romain Guy61c9d4b2010-03-01 14:12:10 -0800335 final View selectedChild = getChildTabViewAt(mSelectedTab);
Evan Millar3730bb12009-08-21 13:58:41 -0700336
Romain Guy65fe2c082010-03-29 12:27:30 -0700337 final Drawable leftStrip = mLeftStrip;
338 final Drawable rightStrip = mRightStrip;
Romain Guy61c9d4b2010-03-01 14:12:10 -0800339
340 leftStrip.setState(selectedChild.getDrawableState());
341 rightStrip.setState(selectedChild.getDrawableState());
Evan Millar3730bb12009-08-21 13:58:41 -0700342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 if (mStripMoved) {
Romain Guy61c9d4b2010-03-01 14:12:10 -0800344 final Rect bounds = mBounds;
345 bounds.left = selectedChild.getLeft();
346 bounds.right = selectedChild.getRight();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 final int myHeight = getHeight();
Romain Guy61c9d4b2010-03-01 14:12:10 -0800348 leftStrip.setBounds(Math.min(0, bounds.left - leftStrip.getIntrinsicWidth()),
349 myHeight - leftStrip.getIntrinsicHeight(), bounds.left, myHeight);
350 rightStrip.setBounds(bounds.right, myHeight - rightStrip.getIntrinsicHeight(),
351 Math.max(getWidth(), bounds.right + rightStrip.getIntrinsicWidth()), myHeight);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 mStripMoved = false;
353 }
Evan Millar3730bb12009-08-21 13:58:41 -0700354
Romain Guy61c9d4b2010-03-01 14:12:10 -0800355 leftStrip.draw(canvas);
356 rightStrip.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 }
358
359 /**
360 * Sets the current tab.
361 * This method is used to bring a tab to the front of the Widget,
362 * and is used to post to the rest of the UI that a different tab
363 * has been brought to the foreground.
Evan Millar3730bb12009-08-21 13:58:41 -0700364 *
365 * Note, this is separate from the traditional "focus" that is
366 * employed from the view logic.
367 *
368 * For instance, if we have a list in a tabbed view, a user may be
369 * navigating up and down the list, moving the UI focus (orange
370 * highlighting) through the list items. The cursor movement does
371 * not effect the "selected" tab though, because what is being
372 * scrolled through is all on the same tab. The selected tab only
373 * changes when we navigate between tabs (moving from the list view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 * to the next tabbed view, in this example).
Evan Millar3730bb12009-08-21 13:58:41 -0700375 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 * To move both the focus AND the selected tab at once, please use
Evan Millar3730bb12009-08-21 13:58:41 -0700377 * {@link #setCurrentTab}. Normally, the view logic takes care of
378 * adjusting the focus, so unless you're circumventing the UI,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 * you'll probably just focus your interest here.
Evan Millar3730bb12009-08-21 13:58:41 -0700380 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 * @param index The tab that you want to indicate as the selected
382 * tab (tab brought to the front of the widget)
Evan Millar3730bb12009-08-21 13:58:41 -0700383 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 * @see #focusCurrentTab
385 */
386 public void setCurrentTab(int index) {
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700387 if (index < 0 || index >= getTabCount() || index == mSelectedTab) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 return;
389 }
390
Gilles Debunne97dfd342010-10-20 16:31:15 -0700391 if (mSelectedTab != -1) {
392 getChildTabViewAt(mSelectedTab).setSelected(false);
393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 mSelectedTab = index;
Jack Veenstra53175142009-06-01 21:27:01 -0700395 getChildTabViewAt(mSelectedTab).setSelected(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 mStripMoved = true;
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700397
398 if (isShown()) {
399 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
400 }
401 }
402
Alan Viverettea54956a2015-01-07 16:05:02 -0800403 /** @hide */
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700404 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800405 public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700406 onPopulateAccessibilityEvent(event);
407 // Dispatch only to the selected tab.
408 if (mSelectedTab != -1) {
Svetoslav Ganov0b0a41d2011-09-07 18:06:03 -0700409 View tabView = getChildTabViewAt(mSelectedTab);
410 if (tabView != null && tabView.getVisibility() == VISIBLE) {
411 return tabView.dispatchPopulateAccessibilityEvent(event);
412 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700413 }
414 return false;
415 }
416
Dianne Hackborna7bb6fb2015-02-03 18:13:40 -0800417 @Override
418 public CharSequence getAccessibilityClassName() {
419 return TabWidget.class.getName();
420 }
421
Alan Viverettea54956a2015-01-07 16:05:02 -0800422 /** @hide */
Svetoslav Ganov736c2752011-04-22 18:30:36 -0700423 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800424 public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) {
425 super.onInitializeAccessibilityEventInternal(event);
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700426 event.setItemCount(getTabCount());
427 event.setCurrentItemIndex(mSelectedTab);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 }
Evan Millar3730bb12009-08-21 13:58:41 -0700429
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800430
Alan Viverettea54956a2015-01-07 16:05:02 -0800431 /** @hide */
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800432 @Override
Alan Viverettea54956a2015-01-07 16:05:02 -0800433 public void sendAccessibilityEventUncheckedInternal(AccessibilityEvent event) {
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800434 // this class fires events only when tabs are focused or selected
435 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && isFocused()) {
436 event.recycle();
437 return;
438 }
Alan Viverettea54956a2015-01-07 16:05:02 -0800439 super.sendAccessibilityEventUncheckedInternal(event);
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 /**
443 * Sets the current tab and focuses the UI on it.
Evan Millar3730bb12009-08-21 13:58:41 -0700444 * This method makes sure that the focused tab matches the selected
445 * tab, normally at {@link #setCurrentTab}. Normally this would not
446 * be an issue if we go through the UI, since the UI is responsible
447 * for calling TabWidget.onFocusChanged(), but in the case where we
448 * are selecting the tab programmatically, we'll need to make sure
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 * focus keeps up.
Evan Millar3730bb12009-08-21 13:58:41 -0700450 *
451 * @param index The tab that you want focused (highlighted in orange)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 * and selected (tab brought to the front of the widget)
Evan Millar3730bb12009-08-21 13:58:41 -0700453 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 * @see #setCurrentTab
455 */
456 public void focusCurrentTab(int index) {
457 final int oldTab = mSelectedTab;
458
459 // set the tab
460 setCurrentTab(index);
Evan Millar3730bb12009-08-21 13:58:41 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 // change the focus if applicable.
463 if (oldTab != index) {
Jack Veenstra53175142009-06-01 21:27:01 -0700464 getChildTabViewAt(index).requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 }
466 }
Evan Millar3730bb12009-08-21 13:58:41 -0700467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 @Override
469 public void setEnabled(boolean enabled) {
470 super.setEnabled(enabled);
Evan Millar3730bb12009-08-21 13:58:41 -0700471
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700472 final int count = getTabCount();
Jack Veenstra53175142009-06-01 21:27:01 -0700473 for (int i = 0; i < count; i++) {
474 View child = getChildTabViewAt(i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475 child.setEnabled(enabled);
476 }
477 }
478
479 @Override
480 public void addView(View child) {
481 if (child.getLayoutParams() == null) {
482 final LinearLayout.LayoutParams lp = new LayoutParams(
483 0,
Romain Guy980a9382010-01-08 15:06:28 -0800484 ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 lp.setMargins(0, 0, 0, 0);
486 child.setLayoutParams(lp);
487 }
488
489 // Ensure you can navigate to the tab with the keyboard, and you can touch it
490 child.setFocusable(true);
491 child.setClickable(true);
492
493 super.addView(child);
494
495 // TODO: detect this via geometry with a tabwidget listener rather
496 // than potentially interfere with the view's listener
Jack Veenstra53175142009-06-01 21:27:01 -0700497 child.setOnClickListener(new TabClickListener(getTabCount() - 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 child.setOnFocusChangeListener(this);
499 }
500
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700501 @Override
Jeff Sharkeycd2ca402011-06-10 15:14:07 -0700502 public void removeAllViews() {
503 super.removeAllViews();
504 mSelectedTab = -1;
505 }
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 /**
508 * Provides a way for {@link TabHost} to be notified that the user clicked on a tab indicator.
509 */
510 void setTabSelectionListener(OnTabSelectionChanged listener) {
511 mSelectionChangedListener = listener;
512 }
513
Jeff Sharkey11f4a482011-08-08 21:05:40 -0700514 /** {@inheritDoc} */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 public void onFocusChange(View v, boolean hasFocus) {
Bjorn Bringertacdef592009-12-10 15:58:49 +0000516 if (v == this && hasFocus && getTabCount() > 0) {
Jack Veenstra53175142009-06-01 21:27:01 -0700517 getChildTabViewAt(mSelectedTab).requestFocus();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 return;
519 }
Evan Millar3730bb12009-08-21 13:58:41 -0700520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 if (hasFocus) {
522 int i = 0;
Jack Veenstra53175142009-06-01 21:27:01 -0700523 int numTabs = getTabCount();
524 while (i < numTabs) {
525 if (getChildTabViewAt(i) == v) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 setCurrentTab(i);
527 mSelectionChangedListener.onTabSelectionChanged(i, false);
Svetoslav Ganov5ac413a2010-10-05 15:59:25 -0700528 if (isShown()) {
529 // a tab is focused so send an event to announce the tab widget state
530 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 break;
533 }
534 i++;
535 }
536 }
537 }
538
539 // registered with each tab indicator so we can notify tab host
540 private class TabClickListener implements OnClickListener {
541
542 private final int mTabIndex;
543
544 private TabClickListener(int tabIndex) {
545 mTabIndex = tabIndex;
546 }
547
548 public void onClick(View v) {
549 mSelectionChangedListener.onTabSelectionChanged(mTabIndex, true);
550 }
551 }
552
553 /**
554 * Let {@link TabHost} know that the user clicked on a tab indicator.
555 */
556 static interface OnTabSelectionChanged {
557 /**
558 * Informs the TabHost which tab was selected. It also indicates
559 * if the tab was clicked/pressed or just focused into.
Evan Millar3730bb12009-08-21 13:58:41 -0700560 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * @param tabIndex index of the tab that was selected
562 * @param clicked whether the selection changed due to a touch/click
563 * or due to focus entering the tab through navigation. Pass true
564 * if it was due to a press/click and false otherwise.
565 */
566 void onTabSelectionChanged(int tabIndex, boolean clicked);
567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568}