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 17 package com.example.android.supportv7.widget; 18 19 import android.os.Bundle; 20 import android.view.ContextMenu; 21 import android.view.Gravity; 22 import android.view.Menu; 23 import android.view.MenuInflater; 24 import android.view.MenuItem; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.Button; 28 import android.widget.TextView; 29 30 import androidx.appcompat.app.AppCompatActivity; 31 import androidx.appcompat.widget.PopupMenu; 32 import androidx.appcompat.widget.SwitchCompat; 33 import androidx.core.view.MenuCompat; 34 import androidx.core.view.MenuItemCompat; 35 36 import com.example.android.supportv7.R; 37 38 import java.text.SimpleDateFormat; 39 import java.util.Date; 40 41 public class PopupMenuActivity extends AppCompatActivity { 42 private TextView mLog; 43 44 private SimpleDateFormat mDateFormat; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 setContentView(R.layout.popup_menu_activity); 51 52 mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 53 54 final ViewGroup container = findViewById(R.id.container); 55 mLog = (TextView) container.findViewById(R.id.log); 56 57 final SwitchCompat elevationToggle = (SwitchCompat) container.findViewById( 58 R.id.elevation_toggle); 59 final Button button = (Button) container.findViewById(R.id.test_button); 60 button.setOnClickListener(new View.OnClickListener() { 61 @Override 62 public void onClick(View v) { 63 // Do we need to use a custom style that removes elevation? 64 boolean useDefaultElevation = elevationToggle.isChecked(); 65 66 PopupMenu popupMenu = null; 67 if (useDefaultElevation) { 68 popupMenu = new PopupMenu(container.getContext(), button); 69 } else { 70 popupMenu = new PopupMenu(container.getContext(), button, Gravity.NO_GRAVITY, 71 0, R.style.CustomPopupNoElevation); 72 } 73 74 final MenuInflater menuInflater = popupMenu.getMenuInflater(); 75 populateMenu(menuInflater, popupMenu.getMenu()); 76 final MenuItem editItem = popupMenu.getMenu().findItem(R.id.action_edit); 77 MenuItemCompat.setContentDescription(editItem, 78 getString(R.string.popup_menu_edit_description)); 79 MenuItemCompat.setTooltipText(editItem, 80 getString(R.string.popup_menu_edit_tooltip)); 81 82 // Register a listener to be notified when a menu item in our popup menu has 83 // been clicked. 84 popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 85 @Override 86 public boolean onMenuItemClick(MenuItem item) { 87 addToLog("Item '"+ item.getTitle() + "' clicked"); 88 return true; 89 } 90 }); 91 92 // Register a listener to be notified when our popup menu is dismissed. 93 popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener() { 94 @Override 95 public void onDismiss(PopupMenu menu) { 96 addToLog("Popup menu dismissed"); 97 } 98 }); 99 100 // Show the popup menu 101 popupMenu.show(); 102 } 103 }); 104 // Long tap will show a context menu which is always a platform (not support) menu. 105 registerForContextMenu(button); 106 } 107 108 @Override 109 public void onCreateContextMenu(ContextMenu menu, View v, 110 ContextMenu.ContextMenuInfo menuInfo) { 111 populateMenu(getMenuInflater(), menu); 112 } 113 114 private void populateMenu(MenuInflater menuInflater, Menu menu) { 115 SwitchCompat dividersToggle = findViewById(R.id.dividers_toggle); 116 menuInflater.inflate(R.menu.popup_menu, menu); 117 MenuCompat.setGroupDividerEnabled(menu, dividersToggle.isChecked()); 118 } 119 120 private void addToLog(String toLog) { 121 String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n"; 122 mLog.setText(toPrepend + mLog.getText()); 123 } 124 } 125