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;
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;
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) {
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;
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
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);
+ }
}
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;
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;
}
}
+ /**
+ * @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());
--- /dev/null
+<?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>
--- /dev/null
+<?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>