PopupMenuActivity.java revision e0c620730fe46d16a22133e0121f2fe9936f1685
1/* 2 * Copyright (C) 2016 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.supportv7.widget; 18 19import android.os.Bundle; 20import android.support.v7.app.AppCompatActivity; 21import android.support.v7.widget.PopupMenu; 22import android.support.v7.widget.SwitchCompat; 23import android.view.Gravity; 24import android.view.MenuInflater; 25import android.view.MenuItem; 26import android.view.View; 27import android.view.ViewGroup; 28import android.widget.Button; 29import android.widget.TextView; 30 31import com.example.android.supportv7.R; 32 33import java.text.SimpleDateFormat; 34import java.util.Date; 35 36public class PopupMenuActivity extends AppCompatActivity { 37 private TextView mLog; 38 39 private SimpleDateFormat mDateFormat; 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 45 setContentView(R.layout.popup_menu_activity); 46 47 mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 48 49 final ViewGroup container = (ViewGroup) findViewById(R.id.container); 50 mLog = (TextView) container.findViewById(R.id.log); 51 52 final SwitchCompat elevationToggle = (SwitchCompat) container.findViewById( 53 R.id.elevation_toggle); 54 final Button button = (Button) container.findViewById(R.id.test_button); 55 button.setOnClickListener(new View.OnClickListener() { 56 @Override 57 public void onClick(View v) { 58 // Do we need to use a custom style that removes elevation? 59 boolean useDefaultElevation = elevationToggle.isChecked(); 60 61 PopupMenu popupMenu = null; 62 if (useDefaultElevation) { 63 popupMenu = new PopupMenu(container.getContext(), button); 64 } else { 65 popupMenu = new PopupMenu(container.getContext(), button, Gravity.NO_GRAVITY, 66 0, R.style.CustomPopupNoElevation); 67 } 68 final MenuInflater menuInflater = popupMenu.getMenuInflater(); 69 menuInflater.inflate(R.menu.popup_menu, popupMenu.getMenu()); 70 71 // Register a listener to be notified when a menu item in our popup menu has 72 // been clicked. 73 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 74 @Override 75 public boolean onMenuItemClick(MenuItem item) { 76 addToLog("Item '"+ item.getTitle() + "' clicked"); 77 return true; 78 } 79 }); 80 81 // Register a listener to be notified when our popup menu is dismissed. 82 popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() { 83 @Override 84 public void onDismiss(PopupMenu menu) { 85 addToLog("Popup menu dismissed"); 86 } 87 }); 88 89 // Show the popup menu 90 popupMenu.show(); 91 } 92 }); 93 } 94 95 private void addToLog(String toLog) { 96 String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n"; 97 mLog.setText(toPrepend + mLog.getText()); 98 } 99} 100