Search code examples
androidlistviewandroid-fragmentsmention

Showing android popout listView


I am trying to implement something similar to facebook/twitter feature which is showing a list of users when you add "@" sign. My problem is i don't know how to implement this kind of pop out or floating view . is it dialog or fragment ?

enter image description here


Solution

  • There is something known as ListPopUpWindow in android which could solve your problem. I have given a example below like how you can initiate it. Create your own listadapter and row xml file. Then pass it to the popup.setAdapter like how you handle the normal list in android.

     private void initiatePopupWindow(View anchor) {
                try {
                    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                    ListPopupWindow popup = new ListPopupWindow(this);
                    popup.setAnchorView(anchor);
                    popup.setWidth((int) (display.getWidth()/(1.5)));
    
                    popup.setAdapter(new CustomAdapterForService(getApplicationContext(), R.layout.rowforservice, listCity));
                    popup.setOnItemClickListener(new OnItemClickListener() {
    
                    @Override
                   public void onItemClick(AdapterView<?> arg0, View view, int position, long id3) {
                  //do what you need to do when you click on a popup list item
                    }
                     });
                    popup.show();
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    

    Finally from where every you need to call it, call it with an Anchor. The anchor is the view, may be button or image or something from where you need to popup the list. Just like this.

    initiatePopupWindow(yourOwnView)
    

    This works for me. If any problem feel free to ask. Thanks.