Popup menu comes out in left bottom
I am trying to implement popup menu in my fragment but the result is far beyond my desire. I have a button on the action bar and one of these buttons call the popup menu and no problem till here. It successfully calls and I can see the popup menu. Here is how I do that.
In fragment class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.x) {
} else showPopup(view /* or getView() */);
return super.onOptionsItemSelected(item);
}
public void showPopup(View v) {
PopupMenu popupMenu = new PopupMenu(getContext(), v);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.event_popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
});
popupMenu.show();
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/a"
android:icon="@drawable/ic_action_back"
android:title="a" />
<item android:id="@+id/b"
android:icon="@drawable/ic_action_back"
android:title="b" />
Fragment's action menu (includes button which opens popup menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/x"
android:icon="@drawable/ic_action_back"
yourapp:showAsAction="ifRoom"
android:title="" />
<item android:id="@+id/y"
android:icon="@drawable/ic_action_menu_dot"
yourapp:showAsAction="ifRoom"
android:title="" />
</menu>
And in the result here is how it looks. I want it to be on top right. How can I handle this. As far as I know, Its default placement is already top right.

java
add a comment |
I am trying to implement popup menu in my fragment but the result is far beyond my desire. I have a button on the action bar and one of these buttons call the popup menu and no problem till here. It successfully calls and I can see the popup menu. Here is how I do that.
In fragment class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.x) {
} else showPopup(view /* or getView() */);
return super.onOptionsItemSelected(item);
}
public void showPopup(View v) {
PopupMenu popupMenu = new PopupMenu(getContext(), v);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.event_popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
});
popupMenu.show();
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/a"
android:icon="@drawable/ic_action_back"
android:title="a" />
<item android:id="@+id/b"
android:icon="@drawable/ic_action_back"
android:title="b" />
Fragment's action menu (includes button which opens popup menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/x"
android:icon="@drawable/ic_action_back"
yourapp:showAsAction="ifRoom"
android:title="" />
<item android:id="@+id/y"
android:icon="@drawable/ic_action_menu_dot"
yourapp:showAsAction="ifRoom"
android:title="" />
</menu>
And in the result here is how it looks. I want it to be on top right. How can I handle this. As far as I know, Its default placement is already top right.

java
add a comment |
I am trying to implement popup menu in my fragment but the result is far beyond my desire. I have a button on the action bar and one of these buttons call the popup menu and no problem till here. It successfully calls and I can see the popup menu. Here is how I do that.
In fragment class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.x) {
} else showPopup(view /* or getView() */);
return super.onOptionsItemSelected(item);
}
public void showPopup(View v) {
PopupMenu popupMenu = new PopupMenu(getContext(), v);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.event_popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
});
popupMenu.show();
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/a"
android:icon="@drawable/ic_action_back"
android:title="a" />
<item android:id="@+id/b"
android:icon="@drawable/ic_action_back"
android:title="b" />
Fragment's action menu (includes button which opens popup menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/x"
android:icon="@drawable/ic_action_back"
yourapp:showAsAction="ifRoom"
android:title="" />
<item android:id="@+id/y"
android:icon="@drawable/ic_action_menu_dot"
yourapp:showAsAction="ifRoom"
android:title="" />
</menu>
And in the result here is how it looks. I want it to be on top right. How can I handle this. As far as I know, Its default placement is already top right.

java
I am trying to implement popup menu in my fragment but the result is far beyond my desire. I have a button on the action bar and one of these buttons call the popup menu and no problem till here. It successfully calls and I can see the popup menu. Here is how I do that.
In fragment class
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.x) {
} else showPopup(view /* or getView() */);
return super.onOptionsItemSelected(item);
}
public void showPopup(View v) {
PopupMenu popupMenu = new PopupMenu(getContext(), v);
MenuInflater menuInflater = popupMenu.getMenuInflater();
menuInflater.inflate(R.menu.event_popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return false;
}
});
popupMenu.show();
}
popup_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/a"
android:icon="@drawable/ic_action_back"
android:title="a" />
<item android:id="@+id/b"
android:icon="@drawable/ic_action_back"
android:title="b" />
Fragment's action menu (includes button which opens popup menu)
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/x"
android:icon="@drawable/ic_action_back"
yourapp:showAsAction="ifRoom"
android:title="" />
<item android:id="@+id/y"
android:icon="@drawable/ic_action_menu_dot"
yourapp:showAsAction="ifRoom"
android:title="" />
</menu>
And in the result here is how it looks. I want it to be on top right. How can I handle this. As far as I know, Its default placement is already top right.

java
java
asked Nov 16 '18 at 11:52
Davis A.Davis A.
208
208
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You need to provide proper anchor view (overflow menu in this case) to the popup menu.
Try changing
showPopup(view);
to
View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);
This should bring the popup menu to top. If its still on the left side use
popupMenu.setGravity(Gravity.END);
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337344%2fpopup-menu-comes-out-in-left-bottom%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You need to provide proper anchor view (overflow menu in this case) to the popup menu.
Try changing
showPopup(view);
to
View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);
This should bring the popup menu to top. If its still on the left side use
popupMenu.setGravity(Gravity.END);
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
add a comment |
You need to provide proper anchor view (overflow menu in this case) to the popup menu.
Try changing
showPopup(view);
to
View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);
This should bring the popup menu to top. If its still on the left side use
popupMenu.setGravity(Gravity.END);
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
add a comment |
You need to provide proper anchor view (overflow menu in this case) to the popup menu.
Try changing
showPopup(view);
to
View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);
This should bring the popup menu to top. If its still on the left side use
popupMenu.setGravity(Gravity.END);
You need to provide proper anchor view (overflow menu in this case) to the popup menu.
Try changing
showPopup(view);
to
View menuItemView = MyActivity.this.findViewById(R.id.overflow_menu); // replace with your id
showPopup(menuItemView);
This should bring the popup menu to top. If its still on the left side use
popupMenu.setGravity(Gravity.END);
answered Nov 16 '18 at 12:08
Rohit5k2Rohit5k2
14.6k42752
14.6k42752
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
add a comment |
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
What is R.id.overflow_menu here? My popup xml? It expects id in this case not menu.
– Davis A.
Nov 16 '18 at 12:25
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
that should be the id of the view you are tapping to open the popup menu, possibly the overflow menu icon id on action bar.
– Rohit5k2
Nov 16 '18 at 12:28
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
It works right now. Thanks.
– Davis A.
Nov 16 '18 at 12:33
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337344%2fpopup-menu-comes-out-in-left-bottom%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown