Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [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 | |
| 17 | package com.android.gallery3d.ui; |
| 18 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.view.Menu; |
| 21 | import android.view.MenuItem; |
| 22 | import android.view.View; |
| 23 | import android.view.View.OnClickListener; |
| 24 | import android.widget.Button; |
| 25 | import android.widget.PopupMenu; |
| 26 | import android.widget.PopupMenu.OnMenuItemClickListener; |
| 27 | |
Owen Lin | 73a04ff | 2012-03-14 17:27:24 +0800 | [diff] [blame] | 28 | import com.android.gallery3d.R; |
| 29 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | |
| 32 | public class CustomMenu implements OnMenuItemClickListener { |
| 33 | @SuppressWarnings("unused") |
| 34 | private static final String TAG = "FilterMenu"; |
| 35 | |
| 36 | public static class DropDownMenu { |
| 37 | private Button mButton; |
| 38 | private PopupMenu mPopupMenu; |
| 39 | private Menu mMenu; |
| 40 | |
| 41 | public DropDownMenu(Context context, Button button, int menuId, |
| 42 | OnMenuItemClickListener listener) { |
| 43 | mButton = button; |
| 44 | mButton.setBackgroundDrawable(context.getResources().getDrawable( |
| 45 | R.drawable.dropdown_normal_holo_dark)); |
| 46 | mPopupMenu = new PopupMenu(context, mButton); |
| 47 | mMenu = mPopupMenu.getMenu(); |
| 48 | mPopupMenu.getMenuInflater().inflate(menuId, mMenu); |
| 49 | mPopupMenu.setOnMenuItemClickListener(listener); |
| 50 | mButton.setOnClickListener(new OnClickListener() { |
| 51 | public void onClick(View v) { |
| 52 | mPopupMenu.show(); |
| 53 | } |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | public MenuItem findItem(int id) { |
| 58 | return mMenu.findItem(id); |
| 59 | } |
| 60 | |
| 61 | public void setTitle(CharSequence title) { |
| 62 | mButton.setText(title); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | private Context mContext; |
| 69 | private ArrayList<DropDownMenu> mMenus; |
| 70 | private OnMenuItemClickListener mListener; |
| 71 | |
| 72 | public CustomMenu(Context context) { |
| 73 | mContext = context; |
| 74 | mMenus = new ArrayList<DropDownMenu>(); |
| 75 | } |
| 76 | |
| 77 | public DropDownMenu addDropDownMenu(Button button, int menuId) { |
| 78 | DropDownMenu menu = new DropDownMenu(mContext, button, menuId, this); |
| 79 | mMenus.add(menu); |
| 80 | return menu; |
| 81 | } |
| 82 | |
| 83 | public void setOnMenuItemClickListener(OnMenuItemClickListener listener) { |
| 84 | mListener = listener; |
| 85 | } |
| 86 | |
Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 87 | public boolean onMenuItemClick(MenuItem item) { |
| 88 | if (mListener != null) { |
| 89 | return mListener.onMenuItemClick(item); |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | } |