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?
Go to app > res > right-click > New > Android Resource Directory and give Directory name and Resource type as menu. Now, we will create a popup_menu file inside that menu resource directory. Go to app > res > menu > right-click > New > Menu Resource File and create a menu resource file and name it as popup_menu.
You can use 9patch to get the shadow and rounded corners for the background. You can either add some styles to the popupMenu and achieve your UI or create popup Window. My conclusion is; if we define a style with parent="@android:style/Widget. PopupMenu" as parent, we override behaviour of our PopupMenu.
To show the popup menu for the view, we need to instantiate Popup constructor and use MenuInflater to load the defined menu resource using MenuInflater. inflate() like as shown below.
Android Popup Menu displays a list of items in a vertical list which presents the view that invoked the menu and is useful to provide an overflow of actions related to specific content.
PopupMenu popup = new PopupMenu(this, v,Gravity.CENTER);
use above code. Gravity have many options like center/left/right check the documentation ocne
As from docs says :
A PopupMenu displays a Menu in a modal popup window anchored to a View. The popup will appear below the anchor view if there is room, or above it if there is not. If the IME is visible the popup will not overlap it until it is touched. Touching outside of the popup will dismiss it.
As I may guess, that "View v"
public void showGenderPopup(View v)
is the TextView you are clicking, which is bound to the method when it is clicked, meaning the PopupMenu will show right below the TextView.
Wouldn't you achieve your goal with a Dialog? For a custom AlertDialog you just need to use method
setView(View v)
of the AlertDialog.Builder , before creating the Dialog itself.
For your custom View you either follow two ways:
XML: Create your XML layout file and then using an inflater to apply the XML Layout over a View customView object. (layout file is called customDialog.xml as an example)
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View customView = inflater.inflate(R.layout.customDialog, null);
RadioButton radioButton = (RadioButton) customView.findViewById(R.id.customDialogRadioButton);
radioButton.setOnClickListener(new OnClickListener() { .. });
DYNAMICALLY :
I'll use LinearLayout as example.
LinearLayout customView = new LinearLayout(context);
RadioButton radioBtn = new RadioButton(context);
radioBtn.setOnClickListener(new OnClickListener() { .. });
customView.addView(radioBtn);
To create the dialog you then use this code
AlertDialog.Builder b = new AlertDialog.Builder(context);
b.setMessage("Example");
// set dialog's parameters from the builder
b.setView(customView);
Dialog d = b.create();
d.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With