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
17package com.example.android.apis.app;
18
19import com.example.android.apis.R;
20
21import android.app.Activity;
22import android.app.Fragment;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.ContextMenu;
26import android.view.LayoutInflater;
27import android.view.Menu;
28import android.view.MenuItem;
29import android.view.View;
30import android.view.ViewGroup;
31import android.view.ContextMenu.ContextMenuInfo;
32import android.widget.Toast;
33
34/**
35 * Demonstration of displaying a context menu from a fragment.
36 */
37public class FragmentContextMenu extends Activity {
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        getFragmentManager().beginTransaction().add(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                    Toast.makeText(getActivity(), "Item 1a was chosen", Toast.LENGTH_SHORT).show();
70                    return true;
71                case R.id.b_item:
72                    Toast.makeText(getActivity(), "Item 1b was chosen", Toast.LENGTH_SHORT).show();
73                    return true;
74            }
75            return super.onContextItemSelected(item);
76        }
77    }
78}
79