When I click on an item of my Popupmenu, the item doesn't get checked and the menu disappear instantly. Is there a way at least to get the item checked ?
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.filtering_menu, popup.getMenu());
popup.getMenu().findItem(R.id.filter_old).setChecked(hide_old);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(getActivity(), item.getTitle(), Toast.LENGTH_SHORT).show();
return false;
}
});
popup.show();
By posting this question, I got the answer, I just need to check the item myself after the click event:
item.setChecked(!item.isChecked());
Related
I am using an image button for a popup menu to popup everything is working fine but when I select an item in the menu the item is selected and it doesn't show the selection so that I could identify the selected item.The checkbox remains unchecked even after the selection
menu_icon_img=myView.findViewById(R.id.Id_customer_over_flow);
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getActivity() != null) {
PopupMenu popup = new PopupMenu(getActivity(), v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
case R.id.select_name_z_a:
if (item.isChecked()) item.setChecked(false);
else item.setChecked(true);
return true;
default:
return false;
}
}
});
}
}
XML
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>
<group
android:checkableBehavior="single"
>
<item
android:id="#+id/select_name_a_z"
android:title="#string/name_a_z"
android:checkable="true"
/>
<item
android:id="#+id/select_name_z_a"
android:title="#string/name_z_a"
android:checkable="true"
/>
</group>
</menu>
The problem is you are creating the popup menu from onClick of an imageview. whenever a click event occures a new instance of popup menu is being created.
to avoid this initiate the popup menu in onCreate method. And call popup.show() from onClick() method.
Toolbar menus are supposed to navigate you the specified activities or fragments on click of the pop up.I am not sure what is the problem , but once you click on any MenuItem it will navigate you to given Intent associated with the the id for example,
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.select_name_a_z:
Intent intent=new Intent(MainActivity.this,SecondActivity.this);
startActivity(intent);
return true;
If you are using a checkable menu item then change the below code from
case R.id.select_name_z_a:
if (item.isChecked())
item.setChecked(false);
else item.setChecked(true);
return true;
Do like
if (!item.isChecked()) item.setChecked(true);
Because item.ischecked() is false in the beginning state.
Made some changes to your above code, Try this
menu_icon_img=findViewById(R.id.Id_customer_over_flow);
popup = new PopupMenu(getApplicationContext(), menu_icon_img);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.sort_menu_items, popup.getMenu());
menu_icon_img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (this != null) {
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Menu menu = popup.getMenu();
for(int i = 0;i<menu.size();i++){
menu.getItem(i).setChecked(false);
}
item.setChecked(true);
return true;
}
});
}
}
});
Also for all items in menu set:
android:checkable="true"
I am using popup menu in my code. It works perfectly . but when I select an item from it, menu was not dismissing .
I tried this . but it is not working
here is my code
PopupMenu popup = new PopupMenu(this, edit1);
//inflating menu from xml resource
popup.inflate(R.menu.options_menu);
popup.getMenu().add("one");
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// if (item.getTitle().equals("one")) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
popup.dismiss();
return false;
}
});
//displaying the popup
popup.show();
Please some one help me.
try this below code it will work for you
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
// if (item.getTitle().equals("one")) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
popup.dismiss();
return true;
}
});
and as I see your comments if you are using onTouchListener for the EditText. just change it to onClickListener and your problem will be solved.
This should be the code block. Returning true shows that the click was handled.
#Override
public boolean onMenuItemClick(MenuItem item) {
// if (item.getTitle().equals("one")) {
Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
In my application I have attached popup menu with each item in listview. The popup menu has further two items when we click on popup menu icon.I have implemented OnMenuItemClickListener in my activity to listen for popup menu item clicks which is working fine.But the problem is that How do I get to know the listitem id (not popup menu item id) when I click on popup menu icon for any listview item.The popup menu code is below:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
return true;
default:
return false;
}
}
Please tell me what is "listitem id" that you want to know? I doubt that it's a "listitem view's id". Probably you're thinking about "position", right?
I don't know where do you call showPopup(View v) from, but you also need to pass the position there:
public void showPopup(View v, int listItemPosition) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
Your goal is to know this position in the onMenuItemClick(MenuItem item) callback.
The simplest way to achieve this is to create variable "listItemPositionForPopupMenu", store this position there and read it in the onMenuItemClick callback:
private int listItemPositionForPopupMenu;
public void showPopup(View v, int listItemPosition) {
listItemPositionForPopupMenu = listItemPosition;
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(this);
popup.inflate(R.menu.actions);
popup.show();
}
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
// read the listItemPositionForPopupMenu here
return true;
default:
return false;
}
}
You can also do it in many other ways, like creating you own OnMenuItemClickListener listener with listItemPosition variable in constructor and create custom interface with onMenuItemClick(MenuItem item, int listItemPosition). Or you can just create an anonymous class, then you don't need to have the listItemPositionForPopupMenu member variable:
public void showPopup(View v, final int listItemPosition) {
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_play:
// read the listItemPosition here
return true;
default:
return false;
}
}
});
popup.inflate(R.menu.actions);
popup.show();
}
I want to make a popup menu that comes from the action bar. So instead of the icon in the action bar there would be something that says menu, and then when clicked it will trigger this popup. Is this possible?
private void showPopupMenu(View v){
popupMenu= new PopupMenu(menuActivity.this, v);
popupMenu.getMenuInflater().inflate(R.menu.action_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
return true;
}
});
popupMenu.show();
}
Try
QuickAction3D
and just specify the menu button as the anchor
I have an ActionBar with an action item on it. After clicking on the action item, I want to show a popup menu.
I implemented this method, but I want to anchor it to the action item or to the ActionBar, not to any view from layout. How to get some kind of view to anchor it from MenuItem?
public boolean onOptionsItemSelected(MenuItem item) {
PopupMenu popupMenu = new PopupMenu(this, ??????); // What view goes here?
popupMenu.inflate(R.menu.counters_overflow);
popupMenu.show();
// ...
return true;
}
So finally I found solution. When you want to anchor popupmenu to ActionItem in ActionBar you need to find view that renders ActionItem.
Simple find view with findViewById() where id is same as id of your menu item in xml.
DISPLAYING POPUP:
public boolean onOptionsItemSelected(MenuItem item) {
// ...
View menuItemView = findViewById(R.id.menu_overflow); // SAME ID AS MENU ID
PopupMenu popupMenu = new PopupMenu(this, menuItemView);
popupMenu.inflate(R.menu.counters_overflow);
// ...
popupMenu.show();
// ...
return true;
}
MENU:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
....
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/ic_overflow"
android:showAsAction="ifRoom"
android:title="#string/menu_overflow"/>
....
</menu>
If menu item is not visible (is in overflow) it does not work. findViewById returns null so you have to check for this situation and anchor to another view.
The accepted answer wasn't working for me, so I found the problem by trial and error.
public boolean onOptionsItemSelected(MenuItem item)
{
View menuItemView = findViewById(item.getItemId());
showPopupMenu(menuItemView)
return true;
}
private void showPopupMenu(View anchor)
{
PopupMenu popup = new PopupMenu(this, anchor);
popup.getMenuInflater().inflate(R.menu.my_popup_menu, popup.getMenu());
popup.show();
}
The key here is that, the item in onOptionsItemSelected(MenuItem item) must be shown on ActionBar. If the item is one of the items that appear when you press the 3 vertical dots on top right of ActionBar, then your app will crash.
plz try this ..
#Override
public boolean onOptionsItemSelected(MenuItem item){
String str=item.getTitle().toString();
Toast.makeText(getBaseContext(), str,Toast.LENGTH_LONG). show();
View view=findViewById(item.getItemId());
switch(view.getId()){
case Menu.FIRST:
showPopup(view); // calling method
}
return super.onOptionsItemSelected(item);
}
// custom method
private void showPopup(final View view) {
PopupMenu popupMenu = new PopupMenu(view.getContext(), view);
popupMenu.getMenu().add(0, 0, Menu.NONE, "Item 1");
popupMenu.getMenu().add(0, 1, Menu.NONE, "Item 2");
popupMenu.getMenu().add(0, 2, Menu.NONE, "Item 3");
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(view.getContext(), item.getTitle() + "clicked", Toast.LENGTH_SHORT).show();
return true;
}
});
popupMenu.show();
}
In addition to the Accepted Answer, the issue of re-inflating the popup on each call to onOptionsItemSelected() method can be simplified by doing it only once and just showing it as many times as we want.
(this works even for custom toolbar inflated via Menu Layout Inflater at runtime. Just keep in mind that the findViewById() to get the Menu item's view can return non-null value only when the view is actually present on the screen, ie, should be visible on the toolbar/actionbar.
Note: If the view is preset in the overflow menu of toolbar/actionBar there might be a chance that the view might get inflated only after the overflow menu was invoked at least once - using 3 dots?)
public class SomeActivity{
private PopupMenu popup;
.... // some code of the activity
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
int menuItem = item.getItemId();
switch (menuItem) {
case R.id.tb_menu_plus:
View menuItemView = findViewById(R.id.tb_menu_plus);
if(popup == null) {
popup = new PopupMenu(this, menuItemView);
popup.inflate(R.menu.dropdown_popup_menu);
}
popup.show();
return true;
}
return super.onOptionsItemSelected(item);
}
If you don't want the menu to be gone when you click specific item return false on that item.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId())
{
case R.id.itemShare_:
onShareClicked();
return true; // the menu will be gone
case R.id.itemCopy_:
onCopyClicked();
return true; // the menu will be gone
case R.id.itemSelectAll_:
onSelectAllClicked();
return false; // the menu will stay
// ...
}
public boolean onOptionsItemSelected(MenuItem item) {
final View addView = getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this).setTitle("Add a Word").setView(addView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.title));
}
}).setNegativeButton("Cancel", null).show();
return (super.onOptionsItemSelected(item));
}
get full source form here..
http://vimaltuts.com/android-tutorial-for-beginners/android-action-bar-tab-menu-example