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