18e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov/* 28e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project 38e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * 48e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License"); 58e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * you may not use this file except in compliance with the License. 68e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * You may obtain a copy of the License at 78e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * 88e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * http://www.apache.org/licenses/LICENSE-2.0 98e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * 108e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * Unless required by applicable law or agreed to in writing, software 118e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS, 128e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * See the License for the specific language governing permissions and 148e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov * limitations under the License. 158e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov */ 168e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 178e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovpackage com.example.android.supportv7.widget; 188e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 198e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.os.Bundle; 208e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.view.LayoutInflater; 218e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.view.View; 228e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.view.ViewGroup; 238e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.AdapterView; 248e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.BaseAdapter; 258e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.Button; 268e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.CheckBox; 278e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.PopupWindow; 288e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport android.widget.TextView; 29def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas 30def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.appcompat.app.AppCompatActivity; 31def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikasimport androidx.appcompat.widget.ListPopupWindow; 32def582a5836579a3fadabfdbe4413cb1652bf098Aurimas Liutikas 338e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport com.example.android.supportv7.R; 348e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 358e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport java.text.SimpleDateFormat; 368e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovimport java.util.Date; 378e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 388e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikovpublic class ListPopupWindowActivity extends AppCompatActivity { 398e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private ViewGroup mContainer; 408e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 418e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private CheckBox mIsModal; 428e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 438e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private TextView mLog; 448e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 458e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private Button mButton; 468e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 478e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private ListPopupWindow mListPopupWindow; 488e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 498e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private BaseAdapter mListPopupAdapter; 508e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 518e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private SimpleDateFormat mDateFormat; 528e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 538e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 548e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov protected void onCreate(Bundle savedInstanceState) { 558e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov super.onCreate(savedInstanceState); 568e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 578e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov setContentView(R.layout.list_popup_window_activity); 588e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 598e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mDateFormat = new SimpleDateFormat("HH:mm:ss.SSS"); 608e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 61fa2e2acf79d791a90410025daad438968550d18cAlan Viverette mContainer = findViewById(R.id.container); 628e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mIsModal = (CheckBox) mContainer.findViewById(R.id.is_modal); 638e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mLog = (TextView) mContainer.findViewById(R.id.log); 648e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mButton = (Button) mContainer.findViewById(R.id.test_button); 658e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 668e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mButton.setOnClickListener(new View.OnClickListener() { 678e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 688e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public void onClick(View v) { 698e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow = new ListPopupWindow(mContainer.getContext()); 708e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 718e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov final String[] POPUP_CONTENT = 728e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov new String[] { "Alice", "Bob", "Charlie", "Deirdre", "El" }; 738e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupAdapter = new BaseAdapter() { 748e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov class ViewHolder { 758e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private TextView title; 768e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private TextView shortcut; 778e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 788e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 798e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 808e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public int getCount() { 818e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov return POPUP_CONTENT.length; 828e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 838e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 848e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 858e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public Object getItem(int position) { 868e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov return POPUP_CONTENT[position]; 878e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 888e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 898e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 908e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public long getItemId(int position) { 918e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov return position; 928e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 938e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 948e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 958e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public View getView(int position, View convertView, ViewGroup parent) { 968e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov if (convertView == null) { 978e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov convertView = LayoutInflater.from(parent.getContext()).inflate( 988e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov R.layout.abc_popup_menu_item_layout, parent, false); 998e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov ViewHolder viewHolder = new ViewHolder(); 1008e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov viewHolder.title = (TextView) convertView.findViewById(R.id.title); 1018e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov viewHolder.shortcut = 1028e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov (TextView) convertView.findViewById(R.id.shortcut); 1038e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov convertView.setTag(viewHolder); 1048e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1058e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1068e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 1078e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov viewHolder.title.setText(POPUP_CONTENT[position]); 1088e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov viewHolder.shortcut.setVisibility(View.GONE); 1098e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov return convertView; 1108e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1118e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov }; 1128e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1138e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.setAdapter(mListPopupAdapter); 1148e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.setAnchorView(mButton); 1158e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 11699046ceeb27c74a3f650513715dd04049afb306dKirill Grouchnikov // Register a listener to be notified when an item in our popup window has 1178e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // been clicked. 1188e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() { 1198e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 1208e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public void onItemClick(AdapterView<?> parent, View view, int position, 1218e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov long id) { 1228e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov addToLog("Item #"+ position + " clicked"); 1238e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov addToLog("Dismissing popup window"); 1248e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.dismiss(); 1258e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1268e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov }); 1278e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1288e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // Register a listener to be notified when our popup window is dismissed. 1298e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { 1308e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 1318e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public void onDismiss() { 1328e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov addToLog("Popup window dismissed"); 1338e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1348e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov }); 1358e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1368e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // Set popup window modality based on the current checkbox state. 1378e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.setModal(mIsModal.isChecked()); 1388e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1398e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // and show it 1408e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mListPopupWindow.show(); 1418e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1428e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov }); 1438e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1448e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // Set up a click listener on the log text view. When the popup window is in modal 1458e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // mode and is dismissed by tapping outside of its bounds *and* over the log text 1468e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov // view bounds, we should *not* get this click listener invoked. 1478e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mLog.setOnClickListener(new View.OnClickListener() { 1488e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov @Override 1498e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov public void onClick(View v) { 1508e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov addToLog("Log view clicked"); 1518e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1528e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov }); 1538e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1548e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov 1558e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov private void addToLog(String toLog) { 1568e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov String toPrepend = mDateFormat.format(new Date()) + " " + toLog + "\n"; 1578e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov mLog.setText(toPrepend + mLog.getText()); 1588e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov } 1598e3d2826356015a413bef273cc99e98bace2eab7Kirill Grouchnikov} 160