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.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.Fragment; 23 import android.os.Bundle; 24 import android.util.Log; 25 import android.view.ContextMenu; 26 import android.view.LayoutInflater; 27 import android.view.Menu; 28 import android.view.MenuItem; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.view.ContextMenu.ContextMenuInfo; 32 33 /** 34 * Demonstration of displaying a context menu from a fragment. 35 */ 36 public class FragmentContextMenu extends Activity { 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 42 // Create the list fragment and add it as our sole content. 43 ContextMenuFragment content = new ContextMenuFragment(); 44 getFragmentManager().beginTransaction().add(android.R.id.content, content).commit(); 45 } 46 47 public static class ContextMenuFragment extends Fragment { 48 49 @Override 50 public View onCreateView(LayoutInflater inflater, ViewGroup container, 51 Bundle savedInstanceState) { 52 View root = inflater.inflate(R.layout.fragment_context_menu, container, false); 53 registerForContextMenu(root.findViewById(R.id.long_press)); 54 return root; 55 } 56 57 @Override 58 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { 59 super.onCreateContextMenu(menu, v, menuInfo); 60 menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A"); 61 menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B"); 62 } 63 64 @Override 65 public boolean onContextItemSelected(MenuItem item) { 66 switch (item.getItemId()) { 67 case R.id.a_item: 68 Log.i("ContextMenu", "Item 1a was chosen"); 69 return true; 70 case R.id.b_item: 71 Log.i("ContextMenu", "Item 1b was chosen"); 72 return true; 73 } 74 return super.onContextItemSelected(item); 75 } 76 } 77 } 78