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