Home | History | Annotate | Download | only in app
      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 import android.widget.Toast;
     33 
     34 /**
     35  * Demonstration of displaying a context menu from a fragment.
     36  */
     37 public 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