0

I'm trying to create this kind of list menu but with no success:

https://i.sstatic.net/farTG.jpg

I would like to use the Android menu button or to press an arrow with "open" or "close" text. That menu will display a list of options :

  • [Icon] + Open/close
  • [Icon] + Take a picture
  • [Icon] + Import pictures from the gallerie
  • [Icon] + Delete picture alrealy sent

But, I've got this kind of result, a menu as block options: https://i.sstatic.net/WZyOz.jpg

I'm using this code from Android developer website :

XML File (/menu/gallerie_menu.xml) :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/open_or_close"
          android:icon="@drawable/ic_open"
          android:title="@string/open"
          android:showAsAction="ifRoom"/>
    <item android:id="@+id/take_pic"
          android:icon="@drawable/ic_camera"
          android:title="@string/take_picture" />
    <item android:id="@+id/import_pic"
          android:icon="@drawable/ic_import"
          android:title="@string/import_picture" />
    <item android:id="@+id/delete"
          android:icon="@drawable/ic_delete"
          android:title="@string/delete_picture" />
</menu>

Java code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.gallerie_menu, menu);
    return true;
}

Could someone advise me or suggest something to me?

1
  • was the answer below of any use?
    – Matt
    Commented Aug 20, 2013 at 10:13

1 Answer 1

1

The only option I can suggest is not a particularly straightforward one.

If inside your activity you declare a PopupWindow variable:

  private PopupWindow myMenu;

Then in the OnCreate of that activity setup your menu window inflate the layout xml file that is the way you want you menu to look and add eventhandlers as necissary:

  View v = getLayoutInflater().inflate(R.layout.test_menu, null, false);
  Button b = (Button)v.findViewById(R.id.myFirstMenuOption);
b.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
        //do something....              
    }           
});//repeat for rest off menu buttons.
    myMenu = new PopupWindow(v, 0, 0, false);
    myMenu.setWidth(LayoutParams.MATCH_PARENT);
    myMenu.setHeight(LayoutParams.WRAP_CONTENT);

Then override the onKeyDown method of your activity to show the menu in response to the button press:

  @Override
public boolean onKeyDown(int keyCode, KeyEvent event) 
{
    if (keyCode == KeyEvent.KEYCODE_MENU) 
    {
        if (myMenu.isShowing()) 
        {
            myMenu.dismiss();
        } 
        else 
        {
            ScrollView sv = (ScrollView)findViewById(R.id.mainLayout);
            myMenu.showAtLocation(sv, android.view.Gravity.BOTTOM | android.view.Gravity.LEFT, 0, 0);
        }
        return true; //swallow the event
    }
    return super.onKeyDown(keyCode, event);
}

The ScrollView here being the topmost view in my activity's layout xml.

There are various options from here on in, for example to code a reusable PopupWindow class of your own, but I leave that to you if this option is what you decide to go with.

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.