1/* 2 * Copyright (C) 2007 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.view; 18 19import com.android.frameworks.coretests.R; 20 21import android.app.ListActivity; 22import android.content.Context; 23import android.os.Bundle; 24import android.util.Log; 25import android.view.ContextMenu; 26import android.view.Menu; 27import android.view.MenuItem; 28import android.view.SubMenu; 29import android.view.View; 30import android.view.ViewGroup; 31import android.view.LayoutInflater; 32import android.view.ContextMenu.ContextMenuInfo; 33import android.widget.AdapterView; 34import android.widget.BaseAdapter; 35import android.widget.TextView; 36 37/** 38 * Exercises context menus in lists 39 */ 40public class ListContextMenu extends ListActivity implements View.OnCreateContextMenuListener 41{ 42 static final String TAG = "ListContextMenu"; 43 44 ThrashListAdapter mAdapter; 45 46 private class ThrashListAdapter extends BaseAdapter { 47 private LayoutInflater mInflater; 48 49 private String[] mTitles = new String[100]; 50 51 public ThrashListAdapter(Context context) { 52 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 53 mTitles = new String[100]; 54 55 int i; 56 for (i=0; i<100; i++) { 57 mTitles[i] = "[" + i + "]"; 58 } 59 } 60 61 public int getCount() { 62 return mTitles.length; 63 } 64 65 public Object getItem(int position) { 66 return position; 67 } 68 69 public long getItemId(int position) { 70 return position; 71 } 72 73 public View getView(int position, View convertView, ViewGroup parent) { 74 TextView view; 75 76 if (convertView == null) { 77 view = (TextView) mInflater.inflate(android.R.layout.simple_list_item_1, null); 78 } else { 79 view = (TextView) convertView; 80 } 81 view.setText("List item " + mTitles[position]); 82 return view; 83 } 84 85 } 86 87 @Override 88 public void onCreate(Bundle icicle) 89 { 90 super.onCreate(icicle); 91 92 mAdapter = new ThrashListAdapter(this); 93 getListView().setOnCreateContextMenuListener(this); 94 setListAdapter(mAdapter); 95 } 96 97 @Override 98 public boolean onCreateOptionsMenu(Menu menu) { 99 MenuItem item = menu.add(0, 0, 0, "Really long menu item name"); 100 item.setTitleCondensed("Long name"); 101 item.setIcon(R.drawable.black_square); 102 103 SubMenu sm = menu.addSubMenu(0, 0, 0, "The 2nd item, a sub menu").setIcon(R.drawable.black_square_stretchable); 104 item = sm.getItem(); 105 item.setTitleCondensed("Sub menu"); 106 sm.add(1, 0, 0, "Subitem 1"); 107 sm.add(1, 0, 0, "Subitem 2"); 108 sm.add(1, 0, 0, "Subitem 3"); 109 sm.setGroupCheckable(1, true, true); 110 menu.add(0, 0, 0, "Item 3"); 111 menu.add(0, 0, 0, "Item 4"); 112 menu.add(0, 0, 0, "Item 5"); 113 menu.add(0, 0, 0, "Item 6"); 114 menu.add(0, 0, 0, "Item 7"); 115 menu.add(0, 0, 0, "Item 8"); 116 menu.add(0, 0, 0, "Item 9"); 117 sm = menu.addSubMenu(0, 0, 0, "Item 10 SM"); 118 sm.add(0, 0, 0, "Subitem 1"); 119 sm.add(0, 0, 0, "Subitem 2"); 120 sm.add(0, 0, 0, "Subitem 3"); 121 sm.add(0, 0, 0, "Subitem 4"); 122 sm.add(0, 0, 0, "Subitem 5"); 123 sm.add(0, 0, 0, "Subitem 6"); 124 sm.add(0, 0, 0, "Subitem 7"); 125 sm.add(0, 0, 0, "Subitem 8"); 126 127 return true; 128 } 129 130 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 131 132 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; 133 134 String text = ((TextView) info.targetView).getText().toString(); 135 if (text.contains("[0]")) { 136 menu.setHeaderTitle("This is a test of the title and the icon").setHeaderIcon( 137 android.R.drawable.sym_def_app_icon); 138 } else if (text.contains("[1]")) { 139 menu.setHeaderTitle("This is a test of just the title"); 140 } else { 141 TextView textView = new TextView(this); 142 textView.setText("This is a test of a custom View"); 143 menu.setHeaderView(textView); 144 } 145 146 menu.add(0, 0, 0, "Test 1"); 147 SubMenu sm = menu.addSubMenu(0, 0, 0, "Test 1.5 SM"); 148 sm.add(0, 0, 0, "CM Subitem 1"); 149 sm.add(0, 0, 0, "CM Subitem 2"); 150 sm.add(0, 0, 0, "CM Subitem 3"); 151 menu.add(0, 0, 0, "Test 2"); 152 menu.add(0, 0, 0, "Test 3"); 153 menu.add(0, 0, 0, "Test 4"); 154 menu.add(0, 0, 0, "Test 5"); 155 menu.add(0, 0, 0, "Test 6"); 156 menu.add(0, 0, 0, "Test 7"); 157 menu.add(0, 0, 0, "Test 8"); 158 menu.add(0, 0, 0, "Test 9"); 159 menu.add(0, 0, 0, "Test 10"); 160 menu.add(0, 0, 0, "Test 11"); 161 menu.add(0, 0, 0, "Test 12"); 162 menu.add(0, 0, 0, "Test 13"); 163 menu.add(0, 0, 0, "Test 14"); 164 menu.add(0, 0, 0, "Test 15"); 165 menu.add(0, 0, 0, "Test 16"); 166 menu.add(0, 0, 0, "Test 17"); 167 menu.add(0, 0, 0, "Test 18"); 168 menu.add(0, 0, 0, "Test 19"); 169 menu.add(0, 0, 0, "Test 20"); 170 menu.add(0, 0, 0, "Test 21"); 171 menu.add(0, 0, 0, "Test 22"); 172 menu.add(0, 0, 0, "Test 23"); 173 menu.add(0, 0, 0, "Test 24"); 174 } 175 176 @Override 177 public boolean onOptionsItemSelected(MenuItem item) { 178 Log.i(TAG, "Options item " + item.toString() + " selected."); 179 180 return super.onOptionsItemSelected(item); 181 } 182 183 @Override 184 public void onOptionsMenuClosed(Menu menu) { 185 Log.i(TAG, "Options menu closed"); 186 } 187 188 @Override 189 public boolean onContextItemSelected(MenuItem item) { 190 Log.i(TAG, "Context item " + item.toString() + " selected."); 191 192 return super.onContextItemSelected(item); 193 } 194 195 @Override 196 public void onContextMenuClosed(Menu menu) { 197 Log.i(TAG, "Context menu closed"); 198 } 199 200 201} 202