Showing Popup Window In Android

Here I am going to tell you how can we show a custom popup listview on clicking on a button or a view in Android, First you need the following class to show popup,
  1. public class CategoryPopUp {  
  2.   
  3.     private PopupWindow popupWindowCategory;  
  4.     ArrayList<String> categories = null;  
  5.     private Context context = null;  
  6.     private SortCategoryChangeListner listner = null;  
  7.   
  8.     public CategoryPopUp(ArrayList<String> categories, Context context, SortCategoryChangeListner listner) {  
  9.   
  10.         this.context = context;  
  11.         this.categories = categories;  
  12.         this.listner = listner;  
  13.     }  
  14.   
  15.     public void showWindow(View view) {  
  16.   
  17.         popupWindowCategory = getPopupWindowCategory(categories);  
  18.         popupWindowCategory.showAsDropDown(view, -50);  
  19.     }  
  20.   
  21.     public PopupWindow getPopupWindowCategory(ArrayList<String> categories) {  
  22.   
  23.         PopupWindow popupWindow = new PopupWindow(context);  
  24.   
  25.         ListView listViewDogs = new ListView(context);  
  26.   
  27.         listViewDogs.setAdapter(categoryAdapter(categories));  
  28.   
  29.         listViewDogs.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
  30.             @Override  
  31.  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {  
  32.   
  33.                 // add some animation when a list item was clicked  
  34.  Animation fadeInAnimation = AnimationUtils.loadAnimation(context, android.R.anim.fade_in);  
  35.                 fadeInAnimation.setDuration(10);  
  36.                 view.startAnimation(fadeInAnimation);  
  37.   
  38.                 // dismiss the pop up  
  39.  popupWindowCategory.dismiss();  
  40.   
  41.                 // get the text and set it as the button text  
  42.  String selectedItemText = ((TextView) view).getText().toString();  
  43.                 if (!TextUtils.isEmpty(selectedItemText))  
  44.                     listner.onSortItemChanged(selectedItemText);  
  45.   
  46.   
  47.             }  
  48.         });  
  49.   
  50.         // some other visual settings  
  51.  popupWindow.setFocusable(true);  
  52.         popupWindow.setWidth(250);  
  53.         popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);  
  54.   
  55.         // set the list view as pop up window content  
  56.  popupWindow.setContentView(listViewDogs);  
  57.   
  58.         return popupWindow;  
  59.     }  
  60.   
  61.     private ArrayAdapter<String> categoryAdapter(ArrayList<String> categories) {  
  62.   
  63.   
  64.         ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, categories) {  
  65.   
  66.             @Override  
  67.  public View getView(int position, View convertView, ViewGroup parent) {  
  68.   
  69.                 // setting the ID and text for every items in the list  
  70.  String item = getItem(position);  
  71.   
  72.                 // visual settings for the list item  
  73.  TextView listItem = new TextView(context);  
  74.   
  75.                 listItem.setText(item);  
  76.                 listItem.setTag(item);  
  77.                 listItem.setTextSize(12);  
  78.                 listItem.setPadding(10101010);  
  79.                 listItem.setTextColor(Color.WHITE);  
  80.   
  81.                 return listItem;  
  82.             }  
  83.         };  
  84.   
  85.         return adapter;  
  86.     }  
  87.   
  88.   
  89. }  
Then in in the Activity class on view click do the following, 
  1. View.OnClickListener sortListner = new View.OnClickListener() {  
  2.     @Override  
  3.  public void onClick(View view) {  
  4.   
  5.         ArrayList<String> categories = new ArrayList<String>();  
  6.         categories.add("all");  
  7.         categories.add("category 1");  
  8.         categories.add("category 2");  
  9.         categories.add("category 3");  
  10.         categories.add("category 4");  
  11.         categories.add("category 5");  
  12.         categories.add("category 6");  
  13.   
  14.         new CategoryPopUp(categories, getActivity(), MyReferralFragment.this).showWindow(view);  
  15.     }  
  16. };