1 /* 2 * Copyright (C) 2015 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.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.AdapterView; 24 import android.widget.BaseAdapter; 25 import android.widget.Button; 26 import android.widget.CheckBox; 27 import android.widget.PopupWindow; 28 import android.widget.TextView; 29 30 import androidx.appcompat.app.AppCompatActivity; 31 import androidx.appcompat.widget.ListPopupWindow; 32 33 import com.example.android.supportv7.R; 34 35 import java.text.SimpleDateFormat; 36 import java.util.Date; 37 38 public class ListPopupWindowActivity extends AppCompatActivity { 39 private ViewGroup mContainer; 40 41 private CheckBox mIsModal; 42 43 private TextView mLog; 44 45 private Button mButton; 46 47 private ListPopupWindow mListPopupWindow; 48 49 private BaseAdapter mListPopupAdapter; 50 51 private SimpleDateFormat mDateFormat; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 57 setContentView(R.layout.list_popup_window_activity); 58 59 mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 60 61 mContainer = findViewById(R.id.container); 62 mIsModal = (CheckBox) mContainer.findViewById(R.id.is_modal); 63 mLog = (TextView) mContainer.findViewById(R.id.log); 64 mButton = (Button) mContainer.findViewById(R.id.test_button); 65 66 mButton.setOnClickListener(new View.OnClickListener() { 67 @Override 68 public void onClick(View v) { 69 mListPopupWindow = new ListPopupWindow(mContainer.getContext()); 70 71 final String[] POPUP_CONTENT = 72 new String[] { "Alice", "Bob", "Charlie", "Deirdre", "El" }; 73 mListPopupAdapter = new BaseAdapter() { 74 class ViewHolder { 75 private TextView title; 76 private TextView shortcut; 77 } 78 79 @Override 80 public int getCount() { 81 return POPUP_CONTENT.length; 82 } 83 84 @Override 85 public Object getItem(int position) { 86 return POPUP_CONTENT[position]; 87 } 88 89 @Override 90 public long getItemId(int position) { 91 return position; 92 } 93 94 @Override 95 public View getView(int position, View convertView, ViewGroup parent) { 96 if (convertView == null) { 97 convertView = LayoutInflater.from(parent.getContext()).inflate( 98 R.layout.abc_popup_menu_item_layout, parent, false); 99 ViewHolder viewHolder = new ViewHolder(); 100 viewHolder.title = (TextView) convertView.findViewById(R.id.title); 101 viewHolder.shortcut = 102 (TextView) convertView.findViewById(R.id.shortcut); 103 convertView.setTag(viewHolder); 104 } 105 106 ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 107 viewHolder.title.setText(POPUP_CONTENT[position]); 108 viewHolder.shortcut.setVisibility(View.GONE); 109 return convertView; 110 } 111 }; 112 113 mListPopupWindow.setAdapter(mListPopupAdapter); 114 mListPopupWindow.setAnchorView(mButton); 115 116 // Register a listener to be notified when an item in our popup window has 117 // been clicked. 118 mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { 119 @Override 120 public void onItemClick(AdapterView<?> parent, View view, int position, 121 long id) { 122 addToLog("Item #"+ position + " clicked"); 123 addToLog("Dismissing popup window"); 124 mListPopupWindow.dismiss(); 125 } 126 }); 127 128 // Register a listener to be notified when our popup window is dismissed. 129 mListPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 130 @Override 131 public void onDismiss() { 132 addToLog("Popup window dismissed"); 133 } 134 }); 135 136 // Set popup window modality based on the current checkbox state. 137 mListPopupWindow.setModal(mIsModal.isChecked()); 138 139 // and show it 140 mListPopupWindow.show(); 141 } 142 }); 143 144 // Set up a click listener on the log text view. When the popup window is in modal 145 // mode and is dismissed by tapping outside of its bounds *and* over the log text 146 // view bounds, we should *not* get this click listener invoked. 147 mLog.setOnClickListener(new View.OnClickListener() { 148 @Override 149 public void onClick(View v) { 150 addToLog("Log view clicked"); 151 } 152 }); 153 } 154 addToLog(String toLog)155 private void addToLog(String toLog) { 156 String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n"; 157 mLog.setText(toPrepend + mLog.getText()); 158 } 159 } 160