diff options
author | 2015-05-26 18:26:36 +0000 | |
---|---|---|
committer | 2015-05-26 18:26:37 +0000 | |
commit | da1b398173a94e94c61a0d186c7dc0ef2a7bd060 (patch) | |
tree | b49b33b03bbfc9cd05f281d96a7628b7e453f8a7 | |
parent | 7bf011ab924f465c7a779f4e42fdd4f9d7c6a09a (diff) | |
parent | b21562c4655cb8dfb73819b68b7fc8eeeab40dac (diff) |
Merge "Support icons in the FloatingToolbar" into mnc-dev
-rw-r--r-- | core/java/com/android/internal/widget/FloatingToolbar.java | 79 | ||||
-rw-r--r-- | core/res/res/layout/floating_popup_menu_image_button.xml | 32 | ||||
-rw-r--r-- | core/res/res/layout/floating_popup_overflow_image_list_item.xml | 32 | ||||
-rw-r--r-- | core/res/res/values/ids.xml | 1 | ||||
-rwxr-xr-x | core/res/res/values/symbols.xml | 3 |
5 files changed, 142 insertions, 5 deletions
diff --git a/core/java/com/android/internal/widget/FloatingToolbar.java b/core/java/com/android/internal/widget/FloatingToolbar.java index b7a53b010946..3cff59a316d4 100644 --- a/core/java/com/android/internal/widget/FloatingToolbar.java +++ b/core/java/com/android/internal/widget/FloatingToolbar.java @@ -26,6 +26,7 @@ import android.graphics.Point; import android.graphics.Rect; import android.graphics.Region; import android.graphics.drawable.ColorDrawable; +import android.text.TextUtils; import android.util.Size; import android.view.Gravity; import android.view.LayoutInflater; @@ -44,6 +45,7 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.ImageButton; +import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; @@ -902,7 +904,7 @@ public final class FloatingToolbar { boolean isFirstItem = true; while (!remainingMenuItems.isEmpty()) { final MenuItem menuItem = remainingMenuItems.peek(); - Button menuItemButton = createMenuItemButton(mContext, menuItem); + View menuItemButton = createMenuItemButton(mContext, menuItem); // Adding additional start padding for the first button to even out button spacing. if (isFirstItem) { @@ -926,8 +928,7 @@ public final class FloatingToolbar { menuItemButton.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); int menuItemButtonWidth = Math.min(menuItemButton.getMeasuredWidth(), toolbarWidth); if (menuItemButtonWidth <= availableWidth) { - menuItemButton.setTag(menuItem); - menuItemButton.setOnClickListener(mMenuItemButtonOnClickListener); + setButtonTagAndClickListener(menuItemButton, menuItem); mContentView.addView(menuItemButton); ViewGroup.LayoutParams params = menuItemButton.getLayoutParams(); params.width = menuItemButtonWidth; @@ -936,7 +937,7 @@ public final class FloatingToolbar { remainingMenuItems.pop(); } else { if (mOpenOverflowButton == null) { - mOpenOverflowButton = (ImageButton) LayoutInflater.from(mContext) + mOpenOverflowButton = LayoutInflater.from(mContext) .inflate(R.layout.floating_popup_open_overflow_button, null); mOpenOverflowButton.setOnClickListener(new View.OnClickListener() { @Override @@ -980,6 +981,15 @@ public final class FloatingToolbar { mContentView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); return new Size(mContentView.getMeasuredWidth(), mContentView.getMeasuredHeight()); } + + private void setButtonTagAndClickListener(View menuItemButton, MenuItem menuItem) { + View button = menuItemButton; + if (isIconOnlyMenuItem(menuItem)) { + button = menuItemButton.findViewById(R.id.floating_toolbar_menu_item_image_button); + } + button.setTag(menuItem); + button.setOnClickListener(mMenuItemButtonOnClickListener); + } } @@ -1141,10 +1151,34 @@ public final class FloatingToolbar { ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); overflowListView.setDivider(null); overflowListView.setDividerHeight(0); + + final int viewTypeCount = 2; + final int stringLabelViewType = 0; + final int iconOnlyViewType = 1; final ArrayAdapter overflowListViewAdapter = new ArrayAdapter<MenuItem>(context, 0) { @Override + public int getViewTypeCount() { + return viewTypeCount; + } + + @Override + public int getItemViewType(int position) { + if (isIconOnlyMenuItem(getItem(position))) { + return iconOnlyViewType; + } + return stringLabelViewType; + } + + @Override public View getView(int position, View convertView, ViewGroup parent) { + if (getItemViewType(position) == iconOnlyViewType) { + return getIconOnlyView(position, convertView); + } + return getStringTitleView(position, convertView); + } + + private View getStringTitleView(int position, View convertView) { TextView menuButton; if (convertView != null) { menuButton = (TextView) convertView; @@ -1157,6 +1191,22 @@ public final class FloatingToolbar { menuButton.setMinimumWidth(mOverflowWidth); return menuButton; } + + private View getIconOnlyView(int position, View convertView) { + View menuButton; + if (convertView != null) { + menuButton = convertView; + } else { + menuButton = LayoutInflater.from(context).inflate( + R.layout.floating_popup_overflow_image_list_item, null); + } + MenuItem menuItem = getItem(position); + ((ImageView) menuButton + .findViewById(R.id.floating_toolbar_menu_item_image_button)) + .setImageDrawable(menuItem.getIcon()); + menuButton.setMinimumWidth(mOverflowWidth); + return menuButton; + } }; overflowListView.setAdapter(overflowListViewAdapter); return overflowListView; @@ -1208,11 +1258,30 @@ public final class FloatingToolbar { } } + /** + * @return {@code true} if the menu item does not not have a string title but has an icon. + * {@code false} otherwise. + */ + private static boolean isIconOnlyMenuItem(MenuItem menuItem) { + if (TextUtils.isEmpty(menuItem.getTitle()) && menuItem.getIcon() != null) { + return true; + } + return false; + } /** * Creates and returns a menu button for the specified menu item. */ - private static Button createMenuItemButton(Context context, MenuItem menuItem) { + private static View createMenuItemButton(Context context, MenuItem menuItem) { + if (isIconOnlyMenuItem(menuItem)) { + View imageMenuItemButton = LayoutInflater.from(context) + .inflate(R.layout.floating_popup_menu_image_button, null); + ((ImageButton) imageMenuItemButton + .findViewById(R.id.floating_toolbar_menu_item_image_button)) + .setImageDrawable(menuItem.getIcon()); + return imageMenuItemButton; + } + Button menuItemButton = (Button) LayoutInflater.from(context) .inflate(R.layout.floating_popup_menu_button, null); menuItemButton.setText(menuItem.getTitle()); diff --git a/core/res/res/layout/floating_popup_menu_image_button.xml b/core/res/res/layout/floating_popup_menu_image_button.xml new file mode 100644 index 000000000000..59341366d8b9 --- /dev/null +++ b/core/res/res/layout/floating_popup_menu_image_button.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* Copyright 2015, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="@dimen/floating_toolbar_menu_button_minimum_width" + android:layout_height="@dimen/floating_toolbar_height" + android:minWidth="@dimen/floating_toolbar_menu_button_minimum_width" + android:minHeight="@dimen/floating_toolbar_height" + android:focusable="false" + android:focusableInTouchMode="false" + android:importantForAccessibility="no"> + <ImageButton + android:id="@+id/floating_toolbar_menu_item_image_button" + android:layout_width="@dimen/floating_toolbar_menu_button_minimum_width" + android:layout_height="@dimen/floating_toolbar_height" + android:scaleType="centerInside" + android:background="?attr/selectableItemBackground" /> +</LinearLayout> diff --git a/core/res/res/layout/floating_popup_overflow_image_list_item.xml b/core/res/res/layout/floating_popup_overflow_image_list_item.xml new file mode 100644 index 000000000000..9988ad5abf58 --- /dev/null +++ b/core/res/res/layout/floating_popup_overflow_image_list_item.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* Copyright 2015, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ +--> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="@dimen/floating_toolbar_menu_button_minimum_width" + android:layout_height="@dimen/floating_toolbar_height" + android:minWidth="@dimen/floating_toolbar_menu_button_minimum_width" + android:minHeight="@dimen/floating_toolbar_height" + android:focusable="false" + android:focusableInTouchMode="false" + android:importantForAccessibility="no"> + <ImageView + android:id="@+id/floating_toolbar_menu_item_image_button" + android:layout_width="@dimen/floating_toolbar_menu_button_minimum_width" + android:layout_height="@dimen/floating_toolbar_height" + android:layout_marginStart="18dp" + android:scaleType="centerInside"/> +</LinearLayout> diff --git a/core/res/res/values/ids.xml b/core/res/res/values/ids.xml index 842c72ef6c96..fca128516001 100644 --- a/core/res/res/values/ids.xml +++ b/core/res/res/values/ids.xml @@ -94,6 +94,7 @@ <item type="id" name="redo" /> <item type="id" name="replaceText" /> <item type="id" name="shareText" /> + <item type="id" name="floating_toolbar_menu_item_image_button" /> <!-- Accessibility action identifier for {@link android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction#ACTION_SHOW_ON_SCREEN}. --> <item type="id" name="accessibilityActionShowOnScreen" /> diff --git a/core/res/res/values/symbols.xml b/core/res/res/values/symbols.xml index 4b57a4776a04..e3033e714087 100755 --- a/core/res/res/values/symbols.xml +++ b/core/res/res/values/symbols.xml @@ -2243,11 +2243,14 @@ <java-symbol type="drawable" name="ic_usb_48dp" /> <!-- Floating toolbar --> + <java-symbol type="id" name="floating_toolbar_menu_item_image_button" /> <java-symbol type="layout" name="floating_popup_container" /> <java-symbol type="layout" name="floating_popup_menu_button" /> <java-symbol type="layout" name="floating_popup_open_overflow_button" /> <java-symbol type="layout" name="floating_popup_close_overflow_button" /> + <java-symbol type="layout" name="floating_popup_menu_image_button" /> <java-symbol type="layout" name="floating_popup_overflow_list_item" /> + <java-symbol type="layout" name="floating_popup_overflow_image_list_item" /> <java-symbol type="dimen" name="floating_toolbar_height" /> <java-symbol type="dimen" name="floating_toolbar_menu_button_side_padding" /> <java-symbol type="dimen" name="floating_toolbar_overflow_side_padding" /> |