1

I am trying to create a PopupWindow that works like an Actionbar Menu like in Chrome for android - example: How to create custom sub menu like Google Chrome application

Here's what I have so far: enter image description here

Currently - I have 3 problems:

  1. I need to position the menu to the right Actionbar button, and below it. like the usual Menus..

  2. I need to have a shadow on the PopupWindow (for some reason it is removed, as I know PopupWindow automatically have this shadow around it)

  3. When clicking outside the PopupWindow, I need it to dismiss like the usual menus of the Actionbar..

Here's my code- On click of the Actionbar button this is the code that runs:

LayoutInflater inflater = (LayoutInflater) MainActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final PopupWindow pw = new PopupWindow(inflater.inflate(
            R.layout.options_menu, null, false), LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
    pw.showAtLocation(findViewById(R.id.main_cont), Gravity.NO_GRAVITY, 50, this.getSupportActionBar().getHeight() + 1);


    ListView moreOptions = (ListView)pw.getContentView().findViewById(R.id.moreOptions);
    String[] options = new String[] { "Settings", "Feedback", "Contribute", "DashClock Integration", "Tutorial", "ViewPager Demo", "Expandable List Demo", "Change Language", "Add 5 to list", "Add 20 to list" };

    final ArrayList<String> moreOptionsList = new ArrayList<String>();
    for (int i = 0; i < options.length; ++i) {
        moreOptionsList.add(options[i]);
    }

    ArrayAdapter<String> moreOptionsAdapter=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            moreOptionsList);
    moreOptions.setAdapter(moreOptionsAdapter);


    //pw.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.listview_bg_inverted));
    /*
    pw.setTouchable(true);
    pw.setFocusable(false);
    */
    pw.setOutsideTouchable(true);

Design of the menu: http://pastebin.com/yi6S2HDt

Thanks in advanced, Din.

Community
  • 1
  • 1
dinbrca
  • 1,101
  • 1
  • 14
  • 30

1 Answers1

0

The PopupWindow has a background (you can change it easily) but no shadow by itself.

The showAtLocation() you actually use allows you to specify the exact position where the window would appear.

To make it dismiss automatically, just call setOutsideTouchable(true); in the constructor.

But having said all that, wouldn't it be better to use a PopupMenu instead of a PopupWindow?

Gábor
  • 9,466
  • 3
  • 65
  • 79