Android popupmenu position

Prakhar Mohan Srivastava picture Prakhar Mohan Srivastava · Jan 22, 2015 · Viewed 31.7k times · Source

I am trying make an android app where clicking a button raises a popupmenu. The popupmenu is being generated but not at the correct position. The code is as follows:

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
    android:id="@+id/genderMale"
    android:title="Male"
/>
<item
    android:id="@+id/genderFemale"
    android:title="Female"
/>
</group>
</menu>

The function to execute the popup is as follows:

public void showGenderPopup(View v)
{
    PopupMenu popup = new PopupMenu(this, v);
    MenuInflater inflater = popup.getMenuInflater();
    inflater.inflate(R.menu.gender_popup, popup.getMenu());
    popup.show();
}

Here the popupmenu is being created just below the textview when I am clicking it. I want it to be generated at the centre of the screen.

How to go about it?

Answer

RamBabu Pudari picture RamBabu Pudari · Jan 22, 2015
  PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER);  

use above code. Gravity have many options like center/left/right check the documentation ocne