MenuItemCompat.java revision 1935ed3af7c6545bc38adfdc6026d87a3249222f
1/* 2 * Copyright (C) 2011 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.support.v4.view; 18 19import android.view.MenuItem; 20import android.view.View; 21 22/** 23 * Helper for accessing newer features in MenuItem. 24 */ 25public class MenuItemCompat { 26 27 /** 28 * Never show this item as a button in an Action Bar. 29 */ 30 public static final int SHOW_AS_ACTION_NEVER = 0; 31 32 /** 33 * Show this item as a button in an Action Bar if the system 34 * decides there is room for it. 35 */ 36 public static final int SHOW_AS_ACTION_IF_ROOM = 1; 37 38 /** 39 * Always show this item as a button in an Action Bar. Use sparingly! 40 * If too many items are set to always show in the Action Bar it can 41 * crowd the Action Bar and degrade the user experience on devices with 42 * smaller screens. A good rule of thumb is to have no more than 2 43 * items set to always show at a time. 44 */ 45 public static final int SHOW_AS_ACTION_ALWAYS = 2; 46 47 /** 48 * When this item is in the action bar, always show it with a 49 * text label even if it also has an icon specified. 50 */ 51 public static final int SHOW_AS_ACTION_WITH_TEXT = 4; 52 53 /** 54 * This item's action view collapses to a normal menu item. 55 * When expanded, the action view temporarily takes over 56 * a larger segment of its container. 57 */ 58 public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8; 59 60 /** 61 * Interface for the full API. 62 */ 63 interface MenuVersionImpl { 64 public boolean setShowAsAction(MenuItem item, int actionEnum); 65 public MenuItem setActionView(MenuItem item, View view); 66 } 67 68 /** 69 * Interface implementation that doesn't use anything about v4 APIs. 70 */ 71 static class BaseMenuVersionImpl implements MenuVersionImpl { 72 @Override 73 public boolean setShowAsAction(MenuItem item, int actionEnum) { 74 return false; 75 } 76 77 @Override 78 public MenuItem setActionView(MenuItem item, View view) { 79 return item; 80 } 81 } 82 83 /** 84 * Interface implementation for devices with at least v11 APIs. 85 */ 86 static class HoneycombMenuVersionImpl implements MenuVersionImpl { 87 @Override 88 public boolean setShowAsAction(MenuItem item, int actionEnum) { 89 MenuItemCompatHoneycomb.setShowAsAction(item, actionEnum); 90 return true; 91 } 92 @Override 93 public MenuItem setActionView(MenuItem item, View view) { 94 return MenuItemCompatHoneycomb.setActionView(item, view); 95 } 96 } 97 98 /** 99 * Select the correct implementation to use for the current platform. 100 */ 101 static final MenuVersionImpl IMPL; 102 static { 103 if (android.os.Build.VERSION.SDK_INT >= 11) { 104 IMPL = new HoneycombMenuVersionImpl(); 105 } else { 106 IMPL = new BaseMenuVersionImpl(); 107 } 108 } 109 110 // ------------------------------------------------------------------- 111 112 /** 113 * Call {@link MenuItem#setShowAsAction(int) MenuItem.setShowAsAction()}. 114 * If running on a pre-{@android.os.Build.VERSION_CODES#HONEYCOMB} device, 115 * does nothing and returns false. Otherwise returns true. 116 */ 117 public static boolean setShowAsAction(MenuItem item, int actionEnum) { 118 return IMPL.setShowAsAction(item, actionEnum); 119 } 120 121 /** 122 * Set an action view for this menu item. An action view will be displayed in place 123 * of an automatically generated menu item element in the UI when this item is shown 124 * as an action within a parent. 125 * 126 * @param view View to use for presenting this item to the user. 127 * @return This Item so additional setters can be called. 128 * 129 * @see #setShowAsAction(MenuItem, int) 130 */ 131 public static MenuItem setActionView(MenuItem item, View view) { 132 return IMPL.setActionView(item, view); 133 } 134} 135