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.view; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.MenuItem; 22 import android.view.View; 23 import android.widget.PopupMenu; 24 import android.widget.Toast; 25 26 import com.example.android.apis.R; 27 28 /** 29 * This demonstrates the use of the PopupMenu class. Clicking the button will inflate and 30 * show a popup menu from an XML resource. 31 */ 32 public class PopupMenu1 extends Activity { 33 @Override onCreate(Bundle savedInstanceState)34 protected void onCreate(Bundle savedInstanceState) { 35 super.onCreate(savedInstanceState); 36 setContentView(R.layout.popup_menu_1); 37 } 38 onPopupButtonClick(View button)39 public void onPopupButtonClick(View button) { 40 PopupMenu popup = new PopupMenu(this, button); 41 popup.getMenuInflater().inflate(R.menu.popup, popup.getMenu()); 42 43 popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 44 public boolean onMenuItemClick(MenuItem item) { 45 Toast.makeText(PopupMenu1.this, "Clicked popup menu item " + item.getTitle(), 46 Toast.LENGTH_SHORT).show(); 47 return true; 48 } 49 }); 50 51 popup.show(); 52 } 53 } 54