Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 17 | package com.android.internal.widget; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 18 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 19 | import com.android.internal.R; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 20 | import com.android.internal.view.menu.ActionMenuItem; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 21 | import com.android.internal.view.menu.ActionMenuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 22 | import com.android.internal.view.menu.MenuBuilder; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 23 | |
| 24 | import android.app.ActionBar; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 25 | import android.app.ActionBar.OnNavigationListener; |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 26 | import android.app.Activity; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 30 | import android.content.pm.PackageManager.NameNotFoundException; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 31 | import android.content.res.TypedArray; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 32 | import android.graphics.drawable.Drawable; |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 33 | import android.text.TextUtils; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 34 | import android.text.TextUtils.TruncateAt; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 35 | import android.util.AttributeSet; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 36 | import android.util.Log; |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 37 | import android.view.ActionMode; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 38 | import android.view.Gravity; |
Adam Powell | 89e0645 | 2010-06-23 20:24:52 -0700 | [diff] [blame] | 39 | import android.view.LayoutInflater; |
| 40 | import android.view.Menu; |
| 41 | import android.view.View; |
| 42 | import android.view.ViewGroup; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 43 | import android.view.ViewParent; |
Adam Powell | 8d12e20 | 2010-11-15 16:44:17 -0800 | [diff] [blame] | 44 | import android.view.Window; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 45 | import android.widget.AdapterView; |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 46 | import android.widget.FrameLayout; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 47 | import android.widget.HorizontalScrollView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 48 | import android.widget.ImageView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 49 | import android.widget.LinearLayout; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 50 | import android.widget.ProgressBar; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 51 | import android.widget.Spinner; |
| 52 | import android.widget.SpinnerAdapter; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 53 | import android.widget.TextView; |
| 54 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 55 | /** |
| 56 | * @hide |
| 57 | */ |
| 58 | public class ActionBarView extends ViewGroup { |
| 59 | private static final String TAG = "ActionBarView"; |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 60 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 61 | /** |
| 62 | * Display options applied by default |
| 63 | */ |
| 64 | public static final int DISPLAY_DEFAULT = 0; |
| 65 | |
| 66 | /** |
| 67 | * Display options that require re-layout as opposed to a simple invalidate |
| 68 | */ |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 69 | private static final int DISPLAY_RELAYOUT_MASK = |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 70 | ActionBar.DISPLAY_SHOW_HOME | |
| 71 | ActionBar.DISPLAY_USE_LOGO | |
| 72 | ActionBar.DISPLAY_HOME_AS_UP | |
| 73 | ActionBar.DISPLAY_SHOW_CUSTOM | |
| 74 | ActionBar.DISPLAY_SHOW_TITLE; |
| 75 | |
| 76 | private static final int DEFAULT_CUSTOM_GRAVITY = Gravity.LEFT | Gravity.CENTER_VERTICAL; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 77 | |
| 78 | private final int mContentHeight; |
| 79 | |
| 80 | private int mNavigationMode; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 81 | private int mDisplayOptions = ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_HOME_AS_UP; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 82 | private CharSequence mTitle; |
| 83 | private CharSequence mSubtitle; |
| 84 | private Drawable mIcon; |
| 85 | private Drawable mLogo; |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 86 | private Drawable mDivider; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 87 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 88 | private View mHomeLayout; |
| 89 | private View mHomeAsUpView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 90 | private ImageView mIconView; |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 91 | private LinearLayout mTitleLayout; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 92 | private TextView mTitleView; |
| 93 | private TextView mSubtitleView; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 94 | private Spinner mSpinner; |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 95 | private LinearLayout mListNavLayout; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 96 | private HorizontalScrollView mTabScrollView; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 97 | private LinearLayout mTabLayout; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 98 | private View mCustomNavView; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 99 | private ProgressBar mProgressView; |
| 100 | private ProgressBar mIndeterminateProgressView; |
| 101 | |
| 102 | private int mProgressBarPadding; |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 103 | private int mItemPadding; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 104 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 105 | private int mTitleStyleRes; |
| 106 | private int mSubtitleStyleRes; |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 107 | private int mProgressStyle; |
| 108 | private int mIndeterminateProgressStyle; |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 109 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 110 | private boolean mShowMenu; |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 111 | private boolean mUserTitle; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 112 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 113 | private MenuBuilder mOptionsMenu; |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 114 | private ActionMenuView mMenuView; |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 115 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 116 | private ActionBarContextView mContextView; |
| 117 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 118 | private ActionMenuItem mLogoNavItem; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 119 | |
| 120 | private SpinnerAdapter mSpinnerAdapter; |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 121 | private OnNavigationListener mCallback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 122 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 123 | private final AdapterView.OnItemSelectedListener mNavItemSelectedListener = |
| 124 | new AdapterView.OnItemSelectedListener() { |
| 125 | public void onItemSelected(AdapterView parent, View view, int position, long id) { |
| 126 | if (mCallback != null) { |
| 127 | mCallback.onNavigationItemSelected(position, id); |
| 128 | } |
| 129 | } |
| 130 | public void onNothingSelected(AdapterView parent) { |
| 131 | // Do nothing |
| 132 | } |
| 133 | }; |
| 134 | |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 135 | private OnClickListener mTabClickListener = null; |
| 136 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 137 | public ActionBarView(Context context, AttributeSet attrs) { |
| 138 | super(context, attrs); |
| 139 | |
Adam Powell | 654e4e4 | 2011-01-03 16:33:50 -0800 | [diff] [blame] | 140 | // Background is always provided by the container. |
| 141 | setBackgroundResource(0); |
| 142 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 143 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar); |
| 144 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 145 | ApplicationInfo appInfo = context.getApplicationInfo(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 146 | PackageManager pm = context.getPackageManager(); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 147 | mNavigationMode = a.getInt(R.styleable.ActionBar_navigationMode, |
| 148 | ActionBar.NAVIGATION_MODE_STANDARD); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 149 | mTitle = a.getText(R.styleable.ActionBar_title); |
| 150 | mSubtitle = a.getText(R.styleable.ActionBar_subtitle); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 151 | |
| 152 | mLogo = a.getDrawable(R.styleable.ActionBar_logo); |
| 153 | if (mLogo == null) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 154 | if (context instanceof Activity) { |
| 155 | try { |
| 156 | mLogo = pm.getActivityLogo(((Activity) context).getComponentName()); |
| 157 | } catch (NameNotFoundException e) { |
| 158 | Log.e(TAG, "Activity component name not found!", e); |
| 159 | } |
| 160 | } |
| 161 | if (mLogo == null) { |
| 162 | mLogo = appInfo.loadLogo(pm); |
| 163 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 164 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 165 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 166 | mIcon = a.getDrawable(R.styleable.ActionBar_icon); |
| 167 | if (mIcon == null) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 168 | if (context instanceof Activity) { |
| 169 | try { |
| 170 | mIcon = pm.getActivityIcon(((Activity) context).getComponentName()); |
| 171 | } catch (NameNotFoundException e) { |
| 172 | Log.e(TAG, "Activity component name not found!", e); |
| 173 | } |
| 174 | } |
| 175 | if (mIcon == null) { |
| 176 | mIcon = appInfo.loadIcon(pm); |
| 177 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 178 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 179 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 180 | final LayoutInflater inflater = LayoutInflater.from(context); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 181 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 182 | final int homeResId = a.getResourceId( |
Adam Powell | b33be1c | 2010-11-18 12:11:40 -0800 | [diff] [blame] | 183 | com.android.internal.R.styleable.ActionBar_homeLayout, |
| 184 | com.android.internal.R.layout.action_bar_home); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 185 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 186 | mHomeLayout = inflater.inflate(homeResId, this, false); |
| 187 | |
| 188 | mHomeAsUpView = mHomeLayout.findViewById(com.android.internal.R.id.up); |
| 189 | mIconView = (ImageView) mHomeLayout.findViewById(com.android.internal.R.id.home); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 190 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 191 | mTitleStyleRes = a.getResourceId(R.styleable.ActionBar_titleTextStyle, 0); |
| 192 | mSubtitleStyleRes = a.getResourceId(R.styleable.ActionBar_subtitleTextStyle, 0); |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 193 | mProgressStyle = a.getResourceId(R.styleable.ActionBar_progressBarStyle, 0); |
| 194 | mIndeterminateProgressStyle = a.getResourceId( |
| 195 | R.styleable.ActionBar_indeterminateProgressStyle, 0); |
| 196 | |
| 197 | mProgressBarPadding = a.getDimensionPixelOffset(R.styleable.ActionBar_progressBarPadding, 0); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 198 | mItemPadding = a.getDimensionPixelOffset(R.styleable.ActionBar_itemPadding, 0); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 199 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 200 | setDisplayOptions(a.getInt(R.styleable.ActionBar_displayOptions, DISPLAY_DEFAULT)); |
| 201 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 202 | final int customNavId = a.getResourceId(R.styleable.ActionBar_customNavigationLayout, 0); |
| 203 | if (customNavId != 0) { |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 204 | mCustomNavView = (View) inflater.inflate(customNavId, this, false); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 205 | mNavigationMode = ActionBar.NAVIGATION_MODE_STANDARD; |
| 206 | setDisplayOptions(mDisplayOptions | ActionBar.DISPLAY_SHOW_CUSTOM); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 209 | mContentHeight = a.getLayoutDimension(R.styleable.ActionBar_height, 0); |
Adam Powell | be4d68e | 2010-10-08 18:16:34 -0700 | [diff] [blame] | 210 | |
| 211 | mDivider = a.getDrawable(R.styleable.ActionBar_divider); |
Adam Powell | 6b336f8 | 2010-08-10 20:13:01 -0700 | [diff] [blame] | 212 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 213 | a.recycle(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 214 | |
Ben Komalo | ad199ec | 2010-11-01 12:24:16 -0700 | [diff] [blame] | 215 | mLogoNavItem = new ActionMenuItem(context, 0, android.R.id.home, 0, 0, mTitle); |
| 216 | mHomeLayout.setOnClickListener(new OnClickListener() { |
Adam Powell | b33be1c | 2010-11-18 12:11:40 -0800 | [diff] [blame] | 217 | public void onClick(View v) { |
| 218 | Context context = getContext(); |
| 219 | if (context instanceof Activity) { |
| 220 | Activity activity = (Activity) context; |
| 221 | activity.onMenuItemSelected(Window.FEATURE_OPTIONS_PANEL, mLogoNavItem); |
| 222 | } |
Ben Komalo | ad199ec | 2010-11-01 12:24:16 -0700 | [diff] [blame] | 223 | } |
Ben Komalo | ad199ec | 2010-11-01 12:24:16 -0700 | [diff] [blame] | 224 | }); |
| 225 | mHomeLayout.setClickable(true); |
| 226 | mHomeLayout.setFocusable(true); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 227 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 228 | |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 229 | public void initProgress() { |
| 230 | mProgressView = new ProgressBar(mContext, null, 0, mProgressStyle); |
| 231 | mProgressView.setId(R.id.progress_horizontal); |
| 232 | mProgressView.setMax(10000); |
| 233 | addView(mProgressView); |
| 234 | } |
| 235 | |
| 236 | public void initIndeterminateProgress() { |
| 237 | mIndeterminateProgressView = new ProgressBar(mContext, null, 0, |
| 238 | mIndeterminateProgressStyle); |
| 239 | mIndeterminateProgressView.setId(R.id.progress_circular); |
| 240 | addView(mIndeterminateProgressView); |
| 241 | } |
| 242 | |
Adam Powell | 9146ac7 | 2010-08-16 18:53:38 -0700 | [diff] [blame] | 243 | @Override |
| 244 | public ActionMode startActionModeForChild(View child, ActionMode.Callback callback) { |
| 245 | // No starting an action mode for an action bar child! (Where would it go?) |
| 246 | return null; |
| 247 | } |
| 248 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 249 | public void setCallback(OnNavigationListener callback) { |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 250 | mCallback = callback; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 251 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 252 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 253 | public void setMenu(Menu menu) { |
Adam Powell | f2d7a5d | 2011-02-07 18:05:24 -0800 | [diff] [blame] | 254 | if (menu == mOptionsMenu) return; |
| 255 | |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 256 | MenuBuilder builder = (MenuBuilder) menu; |
| 257 | mOptionsMenu = builder; |
| 258 | if (mMenuView != null) { |
| 259 | removeView(mMenuView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 260 | } |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 261 | final ActionMenuView menuView = (ActionMenuView) builder.getMenuView( |
| 262 | MenuBuilder.TYPE_ACTION_BUTTON, null); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 263 | final LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 264 | LayoutParams.MATCH_PARENT); |
| 265 | menuView.setLayoutParams(layoutParams); |
| 266 | addView(menuView); |
| 267 | mMenuView = menuView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 268 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 269 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 270 | public boolean showOverflowMenu() { |
| 271 | if (mMenuView != null) { |
| 272 | return mMenuView.showOverflowMenu(); |
| 273 | } |
| 274 | return false; |
| 275 | } |
| 276 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 277 | public void openOverflowMenu() { |
| 278 | if (mMenuView != null) { |
| 279 | mMenuView.openOverflowMenu(); |
| 280 | } |
| 281 | } |
| 282 | |
Adam Powell | 6c6f575 | 2010-08-20 18:34:46 -0700 | [diff] [blame] | 283 | public void postShowOverflowMenu() { |
| 284 | post(new Runnable() { |
| 285 | public void run() { |
| 286 | showOverflowMenu(); |
| 287 | } |
| 288 | }); |
| 289 | } |
| 290 | |
Adam Powell | f75eeb2 | 2010-08-10 15:59:40 -0700 | [diff] [blame] | 291 | public boolean hideOverflowMenu() { |
| 292 | if (mMenuView != null) { |
| 293 | return mMenuView.hideOverflowMenu(); |
| 294 | } |
| 295 | return false; |
| 296 | } |
| 297 | |
Adam Powell | f6148c5 | 2010-08-11 21:10:16 -0700 | [diff] [blame] | 298 | public boolean isOverflowMenuShowing() { |
| 299 | if (mMenuView != null) { |
| 300 | return mMenuView.isOverflowMenuShowing(); |
| 301 | } |
| 302 | return false; |
| 303 | } |
| 304 | |
Adam Powell | 8515ee8 | 2010-11-30 14:09:55 -0800 | [diff] [blame] | 305 | public boolean isOverflowMenuOpen() { |
| 306 | if (mMenuView != null) { |
| 307 | return mMenuView.isOverflowMenuOpen(); |
| 308 | } |
| 309 | return false; |
| 310 | } |
| 311 | |
Adam Powell | 8028dd3 | 2010-07-15 10:16:33 -0700 | [diff] [blame] | 312 | public boolean isOverflowReserved() { |
| 313 | return mMenuView != null && mMenuView.isOverflowReserved(); |
| 314 | } |
| 315 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 316 | public void setCustomNavigationView(View view) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 317 | final boolean showCustom = (mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0; |
| 318 | if (mCustomNavView != null && showCustom) { |
| 319 | removeView(mCustomNavView); |
| 320 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 321 | mCustomNavView = view; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 322 | if (mCustomNavView != null && showCustom) { |
| 323 | addView(mCustomNavView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 324 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 325 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 326 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 327 | public CharSequence getTitle() { |
| 328 | return mTitle; |
| 329 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 330 | |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Set the action bar title. This will always replace or override window titles. |
| 333 | * @param title Title to set |
| 334 | * |
| 335 | * @see #setWindowTitle(CharSequence) |
| 336 | */ |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 337 | public void setTitle(CharSequence title) { |
Adam Powell | e92ea34 | 2010-07-14 14:45:50 -0700 | [diff] [blame] | 338 | mUserTitle = true; |
| 339 | setTitleImpl(title); |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Set the window title. A window title will always be replaced or overridden by a user title. |
| 344 | * @param title Title to set |
| 345 | * |
| 346 | * @see #setTitle(CharSequence) |
| 347 | */ |
| 348 | public void setWindowTitle(CharSequence title) { |
| 349 | if (!mUserTitle) { |
| 350 | setTitleImpl(title); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | private void setTitleImpl(CharSequence title) { |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 355 | mTitle = title; |
| 356 | if (mTitleView != null) { |
| 357 | mTitleView.setText(title); |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 358 | mTitleLayout.setVisibility(TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mSubtitle) ? |
| 359 | GONE : VISIBLE); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 360 | } |
| 361 | if (mLogoNavItem != null) { |
| 362 | mLogoNavItem.setTitle(title); |
| 363 | } |
| 364 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 365 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 366 | public CharSequence getSubtitle() { |
| 367 | return mSubtitle; |
| 368 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 369 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 370 | public void setSubtitle(CharSequence subtitle) { |
| 371 | mSubtitle = subtitle; |
| 372 | if (mSubtitleView != null) { |
| 373 | mSubtitleView.setText(subtitle); |
Adam Powell | 0458796 | 2010-11-11 22:15:07 -0800 | [diff] [blame] | 374 | mSubtitleView.setVisibility(subtitle != null ? VISIBLE : GONE); |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 375 | mTitleLayout.setVisibility(TextUtils.isEmpty(mTitle) && TextUtils.isEmpty(mSubtitle) ? |
| 376 | GONE : VISIBLE); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 379 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 380 | public void setDisplayOptions(int options) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 381 | final int flagsChanged = options ^ mDisplayOptions; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 382 | mDisplayOptions = options; |
| 383 | if ((flagsChanged & DISPLAY_RELAYOUT_MASK) != 0) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 384 | final int vis = (options & ActionBar.DISPLAY_SHOW_HOME) != 0 ? VISIBLE : GONE; |
| 385 | mHomeLayout.setVisibility(vis); |
| 386 | |
| 387 | if ((flagsChanged & ActionBar.DISPLAY_HOME_AS_UP) != 0) { |
| 388 | mHomeAsUpView.setVisibility((options & ActionBar.DISPLAY_HOME_AS_UP) != 0 |
Adam Powell | 8fca37c | 2011-03-04 15:33:18 -0800 | [diff] [blame] | 389 | ? VISIBLE : GONE); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 390 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 391 | |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 392 | if ((flagsChanged & ActionBar.DISPLAY_USE_LOGO) != 0) { |
| 393 | final boolean logoVis = mLogo != null && (options & ActionBar.DISPLAY_USE_LOGO) != 0; |
| 394 | mIconView.setImageDrawable(logoVis ? mLogo : mIcon); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if ((flagsChanged & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
| 398 | if ((options & ActionBar.DISPLAY_SHOW_TITLE) != 0) { |
| 399 | initTitle(); |
| 400 | } else { |
| 401 | removeView(mTitleLayout); |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | if ((flagsChanged & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) { |
| 406 | if ((options & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) { |
| 407 | addView(mCustomNavView); |
| 408 | } else { |
| 409 | removeView(mCustomNavView); |
| 410 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 413 | requestLayout(); |
| 414 | } else { |
| 415 | invalidate(); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | public void setNavigationMode(int mode) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 420 | final int oldMode = mNavigationMode; |
| 421 | if (mode != oldMode) { |
| 422 | switch (oldMode) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 423 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 424 | if (mSpinner != null) { |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 425 | removeView(mListNavLayout); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 426 | } |
| 427 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 428 | case ActionBar.NAVIGATION_MODE_TABS: |
| 429 | if (mTabLayout != null) { |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 430 | removeView(mTabScrollView); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 431 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | switch (mode) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 435 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 436 | if (mSpinner == null) { |
| 437 | mSpinner = new Spinner(mContext, null, |
| 438 | com.android.internal.R.attr.actionDropDownStyle); |
| 439 | mListNavLayout = new LinearLayout(mContext, null, |
| 440 | com.android.internal.R.attr.actionBarTabBarStyle); |
| 441 | LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( |
Adam Powell | e7d4684 | 2011-01-13 21:36:09 -0800 | [diff] [blame] | 442 | LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 443 | params.gravity = Gravity.CENTER; |
| 444 | mListNavLayout.addView(mSpinner, params); |
| 445 | } |
| 446 | if (mSpinner.getAdapter() != mSpinnerAdapter) { |
| 447 | mSpinner.setAdapter(mSpinnerAdapter); |
| 448 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 449 | mSpinner.setOnItemSelectedListener(mNavItemSelectedListener); |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 450 | addView(mListNavLayout); |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 451 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 452 | case ActionBar.NAVIGATION_MODE_TABS: |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 453 | ensureTabsExist(); |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 454 | addView(mTabScrollView); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 455 | break; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 456 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 457 | mNavigationMode = mode; |
| 458 | requestLayout(); |
| 459 | } |
| 460 | } |
| 461 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 462 | private void ensureTabsExist() { |
| 463 | if (mTabScrollView == null) { |
| 464 | mTabScrollView = new HorizontalScrollView(getContext()); |
Adam Powell | c18c38f | 2011-03-15 11:57:46 -0700 | [diff] [blame] | 465 | mTabScrollView.setHorizontalFadingEdgeEnabled(true); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 466 | mTabLayout = new LinearLayout(getContext(), null, |
| 467 | com.android.internal.R.attr.actionBarTabBarStyle); |
| 468 | mTabScrollView.addView(mTabLayout); |
| 469 | } |
| 470 | } |
| 471 | |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 472 | public void setDropdownAdapter(SpinnerAdapter adapter) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 473 | mSpinnerAdapter = adapter; |
| 474 | if (mSpinner != null) { |
| 475 | mSpinner.setAdapter(adapter); |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | public SpinnerAdapter getDropdownAdapter() { |
| 480 | return mSpinnerAdapter; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 481 | } |
Adam Powell | 1780977 | 2010-07-21 13:25:11 -0700 | [diff] [blame] | 482 | |
| 483 | public void setDropdownSelectedPosition(int position) { |
| 484 | mSpinner.setSelection(position); |
| 485 | } |
| 486 | |
| 487 | public int getDropdownSelectedPosition() { |
| 488 | return mSpinner.getSelectedItemPosition(); |
| 489 | } |
| 490 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 491 | public View getCustomNavigationView() { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 492 | return mCustomNavView; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | public int getNavigationMode() { |
| 496 | return mNavigationMode; |
| 497 | } |
| 498 | |
| 499 | public int getDisplayOptions() { |
| 500 | return mDisplayOptions; |
| 501 | } |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 502 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 503 | private TabView createTabView(ActionBar.Tab tab) { |
| 504 | final TabView tabView = new TabView(getContext(), tab); |
| 505 | tabView.setFocusable(true); |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 506 | |
| 507 | if (mTabClickListener == null) { |
| 508 | mTabClickListener = new TabClickListener(); |
| 509 | } |
| 510 | tabView.setOnClickListener(mTabClickListener); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 511 | return tabView; |
| 512 | } |
| 513 | |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 514 | public void addTab(ActionBar.Tab tab, boolean setSelected) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 515 | ensureTabsExist(); |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 516 | View tabView = createTabView(tab); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 517 | mTabLayout.addView(tabView); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 518 | if (setSelected) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 519 | tabView.setSelected(true); |
| 520 | } |
| 521 | } |
| 522 | |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 523 | public void addTab(ActionBar.Tab tab, int position, boolean setSelected) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 524 | ensureTabsExist(); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 525 | final TabView tabView = createTabView(tab); |
| 526 | mTabLayout.addView(tabView, position); |
Adam Powell | 81b8944 | 2010-11-02 17:58:56 -0700 | [diff] [blame] | 527 | if (setSelected) { |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 528 | tabView.setSelected(true); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | public void removeTabAt(int position) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 533 | if (mTabLayout != null) { |
| 534 | mTabLayout.removeViewAt(position); |
| 535 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Adam Powell | 0c24a55 | 2010-11-03 16:44:11 -0700 | [diff] [blame] | 538 | public void removeAllTabs() { |
| 539 | if (mTabLayout != null) { |
| 540 | mTabLayout.removeAllViews(); |
| 541 | } |
| 542 | } |
| 543 | |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 544 | @Override |
| 545 | protected LayoutParams generateDefaultLayoutParams() { |
| 546 | // Used by custom nav views if they don't supply layout params. Everything else |
| 547 | // added to an ActionBarView should have them already. |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 548 | return new ActionBar.LayoutParams(DEFAULT_CUSTOM_GRAVITY); |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 551 | @Override |
| 552 | protected void onFinishInflate() { |
| 553 | super.onFinishInflate(); |
| 554 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 555 | addView(mHomeLayout); |
| 556 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 557 | if (mCustomNavView != null && (mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) { |
| 558 | final ViewParent parent = mCustomNavView.getParent(); |
| 559 | if (parent != this) { |
| 560 | if (parent instanceof ViewGroup) { |
| 561 | ((ViewGroup) parent).removeView(mCustomNavView); |
| 562 | } |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 563 | addView(mCustomNavView); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 564 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 565 | } |
| 566 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 567 | |
| 568 | private void initTitle() { |
| 569 | LayoutInflater inflater = LayoutInflater.from(getContext()); |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 570 | mTitleLayout = (LinearLayout) inflater.inflate(R.layout.action_bar_title_item, null); |
| 571 | mTitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_title); |
| 572 | mSubtitleView = (TextView) mTitleLayout.findViewById(R.id.action_bar_subtitle); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 573 | |
| 574 | if (mTitleStyleRes != 0) { |
| 575 | mTitleView.setTextAppearance(mContext, mTitleStyleRes); |
| 576 | } |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 577 | if (mTitle != null) { |
| 578 | mTitleView.setText(mTitle); |
| 579 | } |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 580 | |
| 581 | if (mSubtitleStyleRes != 0) { |
| 582 | mSubtitleView.setTextAppearance(mContext, mSubtitleStyleRes); |
| 583 | } |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 584 | if (mSubtitle != null) { |
| 585 | mSubtitleView.setText(mSubtitle); |
| 586 | mSubtitleView.setVisibility(VISIBLE); |
| 587 | } |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 588 | |
Adam Powell | 0e94b51 | 2010-06-29 17:58:20 -0700 | [diff] [blame] | 589 | addView(mTitleLayout); |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 590 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 591 | |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 592 | public void setTabSelected(int position) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 593 | ensureTabsExist(); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 594 | final int tabCount = mTabLayout.getChildCount(); |
| 595 | for (int i = 0; i < tabCount; i++) { |
| 596 | final View child = mTabLayout.getChildAt(i); |
| 597 | child.setSelected(i == position); |
| 598 | } |
| 599 | } |
| 600 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 601 | public void setContextView(ActionBarContextView view) { |
| 602 | mContextView = view; |
| 603 | } |
| 604 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 605 | @Override |
| 606 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 607 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); |
| 608 | if (widthMode != MeasureSpec.EXACTLY) { |
| 609 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 610 | "with android:layout_width=\"match_parent\" (or fill_parent)"); |
| 611 | } |
| 612 | |
| 613 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); |
| 614 | if (heightMode != MeasureSpec.AT_MOST) { |
| 615 | throw new IllegalStateException(getClass().getSimpleName() + " can only be used " + |
| 616 | "with android:layout_height=\"wrap_content\""); |
| 617 | } |
| 618 | |
| 619 | int contentWidth = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 620 | |
| 621 | int maxHeight = mContentHeight > 0 ? |
| 622 | mContentHeight : MeasureSpec.getSize(heightMeasureSpec); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 623 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 624 | final int verticalPadding = getPaddingTop() + getPaddingBottom(); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 625 | final int paddingLeft = getPaddingLeft(); |
| 626 | final int paddingRight = getPaddingRight(); |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 627 | final int height = maxHeight - verticalPadding; |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 628 | final int childSpecHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 629 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 630 | int availableWidth = contentWidth - paddingLeft - paddingRight; |
| 631 | int leftOfCenter = availableWidth / 2; |
| 632 | int rightOfCenter = leftOfCenter; |
| 633 | |
| 634 | if (mHomeLayout.getVisibility() != GONE) { |
Ed Heyl | 1c60300 | 2010-11-18 12:18:23 -0800 | [diff] [blame] | 635 | mHomeLayout.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
| 636 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 637 | final int homeWidth = mHomeLayout.getMeasuredWidth(); |
| 638 | availableWidth -= homeWidth; |
| 639 | leftOfCenter -= homeWidth; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 640 | } |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 641 | |
| 642 | if (mMenuView != null) { |
| 643 | availableWidth = measureChildView(mMenuView, availableWidth, |
Adam Powell | 7ade1be | 2010-06-17 12:51:21 -0700 | [diff] [blame] | 644 | childSpecHeight, 0); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 645 | rightOfCenter -= mMenuView.getMeasuredWidth(); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 646 | } |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 647 | |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 648 | boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE && |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 649 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0; |
| 650 | if (showTitle) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 651 | availableWidth = measureChildView(mTitleLayout, availableWidth, childSpecHeight, 0); |
| 652 | leftOfCenter -= mTitleLayout.getMeasuredWidth(); |
| 653 | } |
| 654 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 655 | switch (mNavigationMode) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 656 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 657 | if (mListNavLayout != null) { |
| 658 | final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding; |
| 659 | availableWidth -= itemPaddingSize; |
| 660 | leftOfCenter -= itemPaddingSize; |
| 661 | mListNavLayout.measure( |
Adam Powell | a7db037 | 2010-06-30 17:08:47 -0700 | [diff] [blame] | 662 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 663 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
| 664 | final int listNavWidth = mListNavLayout.getMeasuredWidth(); |
| 665 | availableWidth -= listNavWidth; |
| 666 | leftOfCenter -= listNavWidth; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 667 | } |
| 668 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 669 | case ActionBar.NAVIGATION_MODE_TABS: |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 670 | if (mTabScrollView != null) { |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 671 | final int itemPaddingSize = showTitle ? mItemPadding * 2 : mItemPadding; |
| 672 | availableWidth -= itemPaddingSize; |
| 673 | leftOfCenter -= itemPaddingSize; |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 674 | mTabScrollView.measure( |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 675 | MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
Adam Powell | ca259f4 | 2010-09-08 01:21:42 -0700 | [diff] [blame] | 676 | MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)); |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 677 | final int tabWidth = mTabScrollView.getMeasuredWidth(); |
| 678 | availableWidth -= tabWidth; |
| 679 | leftOfCenter -= tabWidth; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 680 | } |
| 681 | break; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 684 | if (mIndeterminateProgressView != null && |
| 685 | mIndeterminateProgressView.getVisibility() != GONE) { |
| 686 | availableWidth = measureChildView(mIndeterminateProgressView, availableWidth, |
| 687 | childSpecHeight, 0); |
| 688 | rightOfCenter -= mIndeterminateProgressView.getMeasuredWidth(); |
| 689 | } |
| 690 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 691 | if ((mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0 && mCustomNavView != null) { |
| 692 | final LayoutParams lp = generateLayoutParams(mCustomNavView.getLayoutParams()); |
| 693 | final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? |
| 694 | (ActionBar.LayoutParams) lp : null; |
| 695 | |
| 696 | int horizontalMargin = 0; |
| 697 | int verticalMargin = 0; |
| 698 | if (ablp != null) { |
| 699 | horizontalMargin = ablp.leftMargin + ablp.rightMargin; |
| 700 | verticalMargin = ablp.topMargin + ablp.bottomMargin; |
| 701 | } |
| 702 | |
| 703 | // If the action bar is wrapping to its content height, don't allow a custom |
| 704 | // view to MATCH_PARENT. |
| 705 | int customNavHeightMode; |
| 706 | if (mContentHeight <= 0) { |
| 707 | customNavHeightMode = MeasureSpec.AT_MOST; |
| 708 | } else { |
| 709 | customNavHeightMode = lp.height != LayoutParams.WRAP_CONTENT ? |
| 710 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 711 | } |
| 712 | final int customNavHeight = Math.max(0, |
| 713 | (lp.height >= 0 ? Math.min(lp.height, height) : height) - verticalMargin); |
| 714 | |
| 715 | final int customNavWidthMode = lp.width != LayoutParams.WRAP_CONTENT ? |
| 716 | MeasureSpec.EXACTLY : MeasureSpec.AT_MOST; |
| 717 | int customNavWidth = Math.max(0, |
| 718 | (lp.width >= 0 ? Math.min(lp.width, availableWidth) : availableWidth) |
| 719 | - horizontalMargin); |
| 720 | final int hgrav = (ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY) & |
| 721 | Gravity.HORIZONTAL_GRAVITY_MASK; |
| 722 | |
| 723 | // Centering a custom view is treated specially; we try to center within the whole |
| 724 | // action bar rather than in the available space. |
| 725 | if (hgrav == Gravity.CENTER_HORIZONTAL && lp.width == LayoutParams.MATCH_PARENT) { |
| 726 | customNavWidth = Math.min(leftOfCenter, rightOfCenter) * 2; |
| 727 | } |
| 728 | |
| 729 | mCustomNavView.measure( |
| 730 | MeasureSpec.makeMeasureSpec(customNavWidth, customNavWidthMode), |
| 731 | MeasureSpec.makeMeasureSpec(customNavHeight, customNavHeightMode)); |
| 732 | } |
| 733 | |
Adam Powell | e219444 | 2010-08-12 18:13:03 -0700 | [diff] [blame] | 734 | if (mContentHeight <= 0) { |
| 735 | int measuredHeight = 0; |
| 736 | final int count = getChildCount(); |
| 737 | for (int i = 0; i < count; i++) { |
| 738 | View v = getChildAt(i); |
| 739 | int paddedViewHeight = v.getMeasuredHeight() + verticalPadding; |
| 740 | if (paddedViewHeight > measuredHeight) { |
| 741 | measuredHeight = paddedViewHeight; |
| 742 | } |
| 743 | } |
| 744 | setMeasuredDimension(contentWidth, measuredHeight); |
| 745 | } else { |
| 746 | setMeasuredDimension(contentWidth, maxHeight); |
| 747 | } |
| 748 | |
| 749 | if (mContextView != null) { |
| 750 | mContextView.setHeight(getMeasuredHeight()); |
| 751 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 752 | |
| 753 | if (mProgressView != null && mProgressView.getVisibility() != GONE) { |
| 754 | mProgressView.measure(MeasureSpec.makeMeasureSpec( |
| 755 | contentWidth - mProgressBarPadding * 2, MeasureSpec.EXACTLY), |
| 756 | MeasureSpec.makeMeasureSpec(getMeasuredHeight(), MeasureSpec.AT_MOST)); |
| 757 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | private int measureChildView(View child, int availableWidth, int childSpecHeight, int spacing) { |
Adam Powell | a170078 | 2010-05-13 13:27:35 -0700 | [diff] [blame] | 761 | child.measure(MeasureSpec.makeMeasureSpec(availableWidth, MeasureSpec.AT_MOST), |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 762 | childSpecHeight); |
| 763 | |
| 764 | availableWidth -= child.getMeasuredWidth(); |
| 765 | availableWidth -= spacing; |
| 766 | |
| 767 | return availableWidth; |
| 768 | } |
| 769 | |
| 770 | @Override |
| 771 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 772 | int x = getPaddingLeft(); |
| 773 | final int y = getPaddingTop(); |
| 774 | final int contentHeight = b - t - getPaddingTop() - getPaddingBottom(); |
| 775 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 776 | if (mHomeLayout.getVisibility() != GONE) { |
| 777 | x += positionChild(mHomeLayout, x, y, contentHeight); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Adam Powell | 2a7ea67 | 2011-01-23 20:25:15 -0800 | [diff] [blame] | 780 | final boolean showTitle = mTitleLayout != null && mTitleLayout.getVisibility() != GONE && |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 781 | (mDisplayOptions & ActionBar.DISPLAY_SHOW_TITLE) != 0; |
| 782 | if (showTitle) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 783 | x += positionChild(mTitleLayout, x, y, contentHeight); |
| 784 | } |
| 785 | |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 786 | switch (mNavigationMode) { |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 787 | case ActionBar.NAVIGATION_MODE_STANDARD: |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 788 | break; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 789 | case ActionBar.NAVIGATION_MODE_LIST: |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 790 | if (mListNavLayout != null) { |
| 791 | if (showTitle) x += mItemPadding; |
| 792 | x += positionChild(mListNavLayout, x, y, contentHeight) + mItemPadding; |
Adam Powell | a408291 | 2010-06-04 18:34:02 -0700 | [diff] [blame] | 793 | } |
| 794 | break; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 795 | case ActionBar.NAVIGATION_MODE_TABS: |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 796 | if (mTabScrollView != null) { |
Adam Powell | 3f7f7ac | 2010-12-05 16:44:38 -0800 | [diff] [blame] | 797 | if (showTitle) x += mItemPadding; |
| 798 | x += positionChild(mTabScrollView, x, y, contentHeight) + mItemPadding; |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 799 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 800 | break; |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 803 | int menuLeft = r - l - getPaddingRight(); |
Adam Powell | 96675b1 | 2010-06-10 18:58:59 -0700 | [diff] [blame] | 804 | if (mMenuView != null) { |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 805 | positionChildInverse(mMenuView, menuLeft, y, contentHeight); |
| 806 | menuLeft -= mMenuView.getMeasuredWidth(); |
| 807 | } |
| 808 | |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 809 | if (mIndeterminateProgressView != null && |
| 810 | mIndeterminateProgressView.getVisibility() != GONE) { |
| 811 | positionChildInverse(mIndeterminateProgressView, menuLeft, y, contentHeight); |
| 812 | menuLeft -= mIndeterminateProgressView.getMeasuredWidth(); |
| 813 | } |
| 814 | |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 815 | if (mCustomNavView != null && (mDisplayOptions & ActionBar.DISPLAY_SHOW_CUSTOM) != 0) { |
| 816 | LayoutParams lp = mCustomNavView.getLayoutParams(); |
| 817 | final ActionBar.LayoutParams ablp = lp instanceof ActionBar.LayoutParams ? |
| 818 | (ActionBar.LayoutParams) lp : null; |
| 819 | |
| 820 | final int gravity = ablp != null ? ablp.gravity : DEFAULT_CUSTOM_GRAVITY; |
| 821 | final int navWidth = mCustomNavView.getMeasuredWidth(); |
| 822 | |
| 823 | int topMargin = 0; |
| 824 | int bottomMargin = 0; |
| 825 | if (ablp != null) { |
| 826 | x += ablp.leftMargin; |
| 827 | menuLeft -= ablp.rightMargin; |
| 828 | topMargin = ablp.topMargin; |
| 829 | bottomMargin = ablp.bottomMargin; |
| 830 | } |
| 831 | |
| 832 | int hgravity = gravity & Gravity.HORIZONTAL_GRAVITY_MASK; |
| 833 | // See if we actually have room to truly center; if not push against left or right. |
| 834 | if (hgravity == Gravity.CENTER_HORIZONTAL) { |
| 835 | final int centeredLeft = ((mRight - mLeft) - navWidth) / 2; |
| 836 | if (centeredLeft < x) { |
| 837 | hgravity = Gravity.LEFT; |
| 838 | } else if (centeredLeft + navWidth > menuLeft) { |
| 839 | hgravity = Gravity.RIGHT; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | int xpos = 0; |
| 844 | switch (hgravity) { |
| 845 | case Gravity.CENTER_HORIZONTAL: |
| 846 | xpos = ((mRight - mLeft) - navWidth) / 2; |
| 847 | break; |
| 848 | case Gravity.LEFT: |
| 849 | xpos = x; |
| 850 | break; |
| 851 | case Gravity.RIGHT: |
| 852 | xpos = menuLeft - navWidth; |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | int ypos = 0; |
| 857 | switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) { |
| 858 | case Gravity.CENTER_VERTICAL: |
Jeff Sharkey | f786805 | 2010-11-04 12:42:18 -0700 | [diff] [blame] | 859 | final int paddedTop = mTop + getPaddingTop(); |
| 860 | final int paddedBottom = mBottom - getPaddingBottom(); |
| 861 | ypos = ((paddedBottom - paddedTop) - mCustomNavView.getMeasuredHeight()) / 2; |
Adam Powell | 9ab9787 | 2010-10-26 21:47:29 -0700 | [diff] [blame] | 862 | break; |
| 863 | case Gravity.TOP: |
| 864 | ypos = getPaddingTop() + topMargin; |
| 865 | break; |
| 866 | case Gravity.BOTTOM: |
| 867 | ypos = getHeight() - getPaddingBottom() - mCustomNavView.getMeasuredHeight() |
| 868 | - bottomMargin; |
| 869 | break; |
| 870 | } |
| 871 | x += positionChild(mCustomNavView, xpos, ypos, contentHeight); |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 872 | } |
Adam Powell | 6af97e1 | 2010-11-11 21:11:53 -0800 | [diff] [blame] | 873 | |
| 874 | if (mProgressView != null) { |
| 875 | mProgressView.bringToFront(); |
| 876 | final int halfProgressHeight = mProgressView.getMeasuredHeight() / 2; |
| 877 | mProgressView.layout(mProgressBarPadding, -halfProgressHeight, |
| 878 | mProgressBarPadding + mProgressView.getMeasuredWidth(), halfProgressHeight); |
| 879 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | private int positionChild(View child, int x, int y, int contentHeight) { |
| 883 | int childWidth = child.getMeasuredWidth(); |
| 884 | int childHeight = child.getMeasuredHeight(); |
| 885 | int childTop = y + (contentHeight - childHeight) / 2; |
| 886 | |
| 887 | child.layout(x, childTop, x + childWidth, childTop + childHeight); |
| 888 | |
| 889 | return childWidth; |
| 890 | } |
| 891 | |
| 892 | private int positionChildInverse(View child, int x, int y, int contentHeight) { |
| 893 | int childWidth = child.getMeasuredWidth(); |
| 894 | int childHeight = child.getMeasuredHeight(); |
| 895 | int childTop = y + (contentHeight - childHeight) / 2; |
| 896 | |
| 897 | child.layout(x - childWidth, childTop, x, childTop + childHeight); |
| 898 | |
| 899 | return childWidth; |
| 900 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 901 | |
| 902 | private static class TabView extends LinearLayout { |
| 903 | private ActionBar.Tab mTab; |
| 904 | |
| 905 | public TabView(Context context, ActionBar.Tab tab) { |
Adam Powell | ca259f4 | 2010-09-08 01:21:42 -0700 | [diff] [blame] | 906 | super(context, null, com.android.internal.R.attr.actionBarTabStyle); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 907 | mTab = tab; |
| 908 | |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 909 | final View custom = tab.getCustomView(); |
| 910 | if (custom != null) { |
| 911 | addView(custom); |
| 912 | } else { |
| 913 | // TODO Style tabs based on the theme |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 914 | |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 915 | final Drawable icon = tab.getIcon(); |
| 916 | final CharSequence text = tab.getText(); |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 917 | |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 918 | if (icon != null) { |
| 919 | ImageView iconView = new ImageView(context); |
| 920 | iconView.setImageDrawable(icon); |
| 921 | LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 922 | LayoutParams.WRAP_CONTENT); |
| 923 | lp.gravity = Gravity.CENTER_VERTICAL; |
| 924 | iconView.setLayoutParams(lp); |
| 925 | addView(iconView); |
| 926 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 927 | |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 928 | if (text != null) { |
Adam Powell | ca259f4 | 2010-09-08 01:21:42 -0700 | [diff] [blame] | 929 | TextView textView = new TextView(context, null, |
| 930 | com.android.internal.R.attr.actionBarTabTextStyle); |
Adam Powell | 2b6230e | 2010-09-07 17:55:25 -0700 | [diff] [blame] | 931 | textView.setText(text); |
| 932 | textView.setSingleLine(); |
| 933 | textView.setEllipsize(TruncateAt.END); |
| 934 | LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 935 | LayoutParams.WRAP_CONTENT); |
| 936 | lp.gravity = Gravity.CENTER_VERTICAL; |
| 937 | textView.setLayoutParams(lp); |
| 938 | addView(textView); |
| 939 | } |
Adam Powell | 661c908 | 2010-07-02 10:09:44 -0700 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
| 943 | LayoutParams.MATCH_PARENT, 1)); |
| 944 | } |
| 945 | |
| 946 | public ActionBar.Tab getTab() { |
| 947 | return mTab; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | private class TabClickListener implements OnClickListener { |
| 952 | public void onClick(View view) { |
| 953 | TabView tabView = (TabView) view; |
| 954 | tabView.getTab().select(); |
| 955 | final int tabCount = mTabLayout.getChildCount(); |
| 956 | for (int i = 0; i < tabCount; i++) { |
| 957 | final View child = mTabLayout.getChildAt(i); |
| 958 | child.setSelected(child == view); |
| 959 | } |
| 960 | } |
| 961 | } |
Adam Powell | 2b0952b | 2011-03-09 00:15:38 -0800 | [diff] [blame] | 962 | |
| 963 | private static class HomeView extends FrameLayout { |
| 964 | private View mUpView; |
| 965 | private View mIconView; |
| 966 | |
| 967 | public HomeView(Context context) { |
| 968 | this(context, null); |
| 969 | } |
| 970 | |
| 971 | public HomeView(Context context, AttributeSet attrs) { |
| 972 | super(context, attrs); |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | protected void onFinishInflate() { |
| 977 | mUpView = findViewById(com.android.internal.R.id.up); |
| 978 | mIconView = (ImageView) findViewById(com.android.internal.R.id.home); |
| 979 | } |
| 980 | |
| 981 | @Override |
| 982 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 983 | measureChildWithMargins(mUpView, widthMeasureSpec, 0, heightMeasureSpec, 0); |
| 984 | final LayoutParams upLp = (LayoutParams) mUpView.getLayoutParams(); |
| 985 | int width = upLp.leftMargin + mUpView.getMeasuredWidth() + upLp.rightMargin; |
| 986 | int height = upLp.topMargin + mUpView.getMeasuredHeight() + upLp.bottomMargin; |
| 987 | measureChildWithMargins(mIconView, widthMeasureSpec, width, heightMeasureSpec, 0); |
| 988 | final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); |
| 989 | width += iconLp.leftMargin + mIconView.getMeasuredWidth() + iconLp.rightMargin; |
| 990 | height = Math.max(height, |
| 991 | iconLp.topMargin + mIconView.getMeasuredHeight() + iconLp.bottomMargin); |
| 992 | setMeasuredDimension(width, height); |
| 993 | } |
| 994 | |
| 995 | @Override |
| 996 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 997 | final int vCenter = (b - t) / 2; |
| 998 | int width = r - l; |
| 999 | if (mUpView.getVisibility() != GONE) { |
| 1000 | final LayoutParams upLp = (LayoutParams) mUpView.getLayoutParams(); |
| 1001 | final int upHeight = mUpView.getMeasuredHeight(); |
| 1002 | final int upWidth = mUpView.getMeasuredWidth(); |
| 1003 | final int upTop = t + vCenter - upHeight / 2; |
| 1004 | mUpView.layout(l, upTop, l + upWidth, upTop + upHeight); |
| 1005 | final int upOffset = upLp.leftMargin + upWidth + upLp.rightMargin; |
| 1006 | width -= upOffset; |
| 1007 | l += upOffset; |
| 1008 | } |
| 1009 | final LayoutParams iconLp = (LayoutParams) mIconView.getLayoutParams(); |
| 1010 | final int iconHeight = mIconView.getMeasuredHeight(); |
| 1011 | final int iconWidth = mIconView.getMeasuredWidth(); |
| 1012 | final int hCenter = (r - l) / 2; |
| 1013 | final int iconLeft = l + iconLp.leftMargin + hCenter - iconWidth / 2; |
| 1014 | final int iconTop = t + iconLp.topMargin + vCenter - iconHeight / 2; |
| 1015 | mIconView.layout(iconLeft, iconTop, iconLeft + iconWidth, iconTop + iconHeight); |
| 1016 | } |
| 1017 | } |
Adam Powell | 33b9743 | 2010-04-20 10:01:14 -0700 | [diff] [blame] | 1018 | } |