0

I've a problem to create a popup menu on Android. Pratically, I want to load dynamically the popup but when I execute the app, the menu is out of screen and doesn't anchored on button...why?

This is a screen to understand:

enter image description here

enter image description here

This is a summary code that I use to create my popupmenu:

public class MainActivity extends FragmentActivity
{
    PopupMenu select_job;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
         //...
         View view = inflater.inflate(R.layout.my_custom_layout ,null);
         select_job = new PopupMenu(this, view);
         select_job.getMenu().add("ProvaLavoro");
    }

    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch(item.getItemId())
        {
            case R.id.popupmenu:
                select_job.show();
                break;
        }
    }
}

This is my_custom_layout.xml to inflate whitin:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/txtNameJobND"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:textColor="#111"
        android:textStyle="bold"/>

</RelativeLayout>

What am I doing wrong?

Thanks!!

user3449772
  • 749
  • 1
  • 14
  • 27

1 Answers1

2
         select_job = new PopupMenu(this, view);

Here, view is the Anchor for the popup menu. You should pass the MenuItem, (R.id.popupmenu) as the anchor.

I haven't done it myself (anchored a PopupMenu to a Actionbar item) but that should be the idea behind it, I think.

From the Android Documents:

public PopupMenu (Context context, View anchor)

Added in API level 11

Construct a new PopupMenu.

Parameters

context : Context for the PopupMenu.

anchor : Anchor view for this popup. The popup will appear below the anchor if there is room, or above it if there is not.

Lev
  • 443
  • 1
  • 4
  • 14
  • thanks! but I have an other question: how can I pass the menuitem in the view? PopupMenu accepts only a view (R.id.popupmenu is not a view)... – user3449772 Jun 12 '14 at 13:46
  • I found the answer here, I think :) [link](http://stackoverflow.com/questions/14729592/show-popup-menu-on-actionbar-item-click) – Lev Jun 12 '14 at 13:49
  • Thanks! I will try later! I'm not at home now :) – user3449772 Jun 12 '14 at 16:02