To create a popup menu with an icon in Android using Java, you can follow these steps:
First, create an XML layout file for the popup menu. Let's name it popup_menu.xml. In this file, define the menu items and their icons. Here's an example:
In your activity or fragment, create a method to show the popup menu. Let's name it showPopupMenu(). Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void showPopup() { if(scheduleDetailsAndCHRS.size()!=0){ PopupMenu popup = new PopupMenu(getContext(), binding.imageviewAdminHomeSearchIcon); popup.getMenuInflater().inflate(R.menu.popup_director_search_menu, popup.getMenu()); Object menuHelper; Class[] argTypes; ArrayList<ScheduleDetailsAndCHR> filteredList = new ArrayList<>(); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.action_Director_Name: // call member class or function here for (ScheduleDetailsAndCHR checkItem : scheduleDetailsAndCHRS) { if (checkItem.getTeacherName().toLowerCase().contains(binding.editTextAdminHomeSearch.getText().toString().toLowerCase())) { filteredList.add(checkItem); } } adapter.filterList(filteredList); if(filteredList.size()==0){
for (ScheduleDetailsAndCHR checkItem : scheduleDetailsAndCHRS) { if (checkItem.getCourseName().toLowerCase().contains(binding.editTextAdminHomeSearch.getText().toString().toLowerCase())) { filteredList.add(checkItem); } } adapter.filterList(filteredList); if(filteredList.size()==0){
Toast.makeText(getContext(), "Not Found", Toast.LENGTH_SHORT).show(); } break; case R.id.action_Director_Date: for (ScheduleDetailsAndCHR checkItem : scheduleDetailsAndCHRS) { if (checkItem.getDate().toLowerCase().contains(binding.editTextAdminHomeSearch.getText().toString().toLowerCase())) { filteredList.add(checkItem); } } adapter.filterList(filteredList); if(filteredList.size()==0){
Toast.makeText(getContext(), "Not Found", Toast.LENGTH_SHORT).show(); } // call My Buddies class or function here break; case R.id.action_Director_Discipline: for (ScheduleDetailsAndCHR checkItem : scheduleDetailsAndCHRS) { if (checkItem.getDiscipline().toLowerCase().contains(binding.editTextAdminHomeSearch.getText().toString().toLowerCase())) { filteredList.add(checkItem); } } adapter.filterList(filteredList); if(filteredList.size()==0){
Toast.makeText(getContext(), "Not Found", Toast.LENGTH_SHORT).show(); } // call My Buddies class or function here return true; } return true; } }); popup.show(); }
}
RecyclerView Search throw popup menu
1
2
3
4
public void filterList(ArrayList<ScheduleDetailsAndCHR> filteredList) {