I have implemented popup menu in button onClick, with AppCompact theme I can get the menu overflow with empty items in menu. please help me on this
OptionMenuBtn = (ImageButton) v.findViewById(R.id.three_dot);
OptionMenuBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated m// openOptionsMenu();
showPopup(v);
}
});
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(MainActivity.this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.action_menu, popup.getMenu());
popup.show();
}
this should be the Xml file :
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_edit"
android:title="#string/menu_edit" />
<item
android:id="#+id/menu_block"
android:title="#string/menu_deactivate" /></menu>
and the code in activity on your onclick
PopupMenu popupMenu = new PopupMenu(YourActivity.this, view);
popupMenu.setOnMenuItemClickListener(YourActivity.this);
popupMenu.inflate(R.menu.menu_import_export);
popupMenu.show();
and implements :
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_edit:
//TODO
return true;
case R.id.menu_block:
//TODO
return true;
}
return false;
}
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"
In my android app I have a menu option which works when the device has dedicated hardware menu option. for another device I don't have dedicated Menu button so I tried to add a button and on click of that a popup will display the menu, The popup is working but not displaying the options, and further to that how to work on the selected popup option.
My button layout is as follows:
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/ic_overflow_holo_dark"
android:contentDescription="#string/descr_overflow_button"
android:onClick="showPopup" />
This is my code to show the popup:
public boolean showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.emailmenu, popup.getMenu());
popup.show();
return true;
}
And this my option code:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/bluebutton" android:icon="#drawable/icon_blue_btn"
android:title="" />
<item android:id="#+id/zephyr" android:icon="#drawable/icon_zephyrme"
android:title="" />
<item android:id="#+id/skype" android:icon="#drawable/icon_skype"
android:title="" />
</menu>
I all ready have this onCreateOptionsMenu() for triggering the option from dedicated menu key. How to get it to work from popup.
If you want to show Icons instead of Title, then create your PopupMenu like below
The method setForceShowIcon(menu); will force the PopMenu to show icons.
you can have text with icons too.
private void showPopupMenu(){
PopupMenu menu=new PopupMenu(this,anchorView);
menu.getMenuInflater().inflate(R.menu.popup_menu,menu.getMenu());
setForceShowIcon(menu);
menu.setOnMenuItemClickListener(menuClickListner);
menu.show();
}
public static void setForceShowIcon(PopupMenu popupMenu) {
try {
Field[] fields = popupMenu.getClass().getDeclaredFields();
for (Field field : fields) {
if ("mPopup".equals(field.getName())) {
field.setAccessible(true);
Object menuPopupHelper = field.get(popupMenu);
Class<?> classPopupHelper = Class.forName(menuPopupHelper
.getClass().getName());
Method setForceIcons = classPopupHelper.getMethod(
"setForceShowIcon", boolean.class);
setForceIcons.invoke(menuPopupHelper, true);
break;
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
//This is Menu click listner
PopupMenu.OnMenuItemClickListener menuClickListner = new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()){
case R.id.some_id1:
//actions here
break;
case R.id.some_id2:
//actions here..
break;
case R.id.some_id3:
break;
}
return false;
}
};
Hope this helps!
try like this
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(MainActivity.this, SecounActivity.class));
return true;
}
});
popup.show();
I search a lot on net but there is nothing about preventing popup menu from closing.
Whenever i click on checkbox item or any other popup menu item, popup menu dismiss itself. How can i prevent it from dismissing when user check/uncheck checkbox in popup menu.
I'm showing popup menu on actionbar-menu item's click event.
//main_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.worldofjobs.woj.MainActivity" >
<item
android:id="#+id/action_popUpMenu"
android:icon="#drawable/ic_action_overflow"
android:title="#string/main_action_popUpMenu"
app:showAsAction="always"/>
</menu>
//popup_items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/refresh_item"
android:title="#string/main_refresh"/>
<item
android:id="#+id/checkbox_item"
android:checkable="true"
android:title="Start notification"/>
<item
android:id="#+id/changePasswrod_item"
android:title="#string/main_changePassword"/>
<item
android:id="#+id/deleteAccount_item"
android:title="#string/main_deleteAccount"/>
<item
android:id="#+id/logout_item"
android:title="#string/main_logout"/>
</menu>
/**
* Shows popup menu on click of action bar-menu inflates from
* menu.pop_items-xml
*/
private void showPopup() {
try {
View v = findViewById(R.id.action_popUpMenu);
PopupMenu popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(MainActivity.this);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.pop_items, popup.getMenu());
popup.show();
} catch (Exception e) {
Log.e("MainActivity-showPopup:", e.toString());
}
}
/**
* Handles click events of popup menu items
*/
#Override
public boolean onMenuItemClick(MenuItem item) {
super.onMenuItemSelected(1, item);
switch (item.getItemId()) {
case R.id.refresh_item:
refresh();
return true;
case R.id.checkbox_item:
return true;
case R.id.changePasswrod_item:
changePasswordPopup();
return true;
case R.id.deleteAccount_item:
deleteAccount();
return true;
case R.id.logout_item:
session.logout();
finish();
return true;
}
return true;
}
Using popupMenu.show() to immediately re-show the popup menu does not work correctly with checkable menu items when changing their checked states.
Here a method that prevents closing the popup menu in the first place. Make sure that onMenuItemClick returns false.
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
item.setChecked(!item.isChecked());
// Do other stuff
// Keep the popup menu open
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(context));
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
return false;
}
});
The trick here is to show the menu right after it dismisses.
Below is a sample code snippet:
popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId()==R.id.search_by_date_checkbox){
item.setChecked(!item.isChecked());
}
//This is the trick here!!!!
popupMenu.show();
return true;
}
});
You can try this trick with your code! This is how I did it. :)
Oliver's answer above (https://stackoverflow.com/a/31727213/2423194) gave me a crash, and its message told me to use MenuItemCompat instead. After some tweaking to this code, it works:
// Keep the popup menu open
item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
item.setActionView(new View(getContext()));
MenuItemCompat.setOnActionExpandListener(item, new MenuItemCompat.OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem item) {
return false;
}
});
Thanks Oliver!
Try declaring the PopupMenu globally and calling popup.show(); before returning true in onMenuItemClick function.
Got it working by adding popup.show(); on the click of the button and at the end of the click.
final ImageButton layerButton = (ImageButton) findViewById(R.id.layers);
final PopupMenu popup = new PopupMenu(MapsActivity.this, layerButton);
popup.getMenuInflater().inflate(R.menu.toolbar_menu, popup.getMenu());
layerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
handleClicksOnCheckBoxes(item);
return true;
}
});
//And here
popup.show();
}
});
However, this is not an optimal solution because if the list has so many items that it will be possible to scroll the list, the list will be scrolled to the top when clicking on an item.
In your case R.id.checkbox_item
return false;
This will tell the system that the event has not yet been handeled and it will not dimiss the menu. See HERE
I want to make something like that:
I try to make the popup menu whit the collapseview, y the action bar. but i try all the thing that i found on internet and no solution found. i didnt need suppoart android api less than 15.
this the main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
<item android:id="#+id/action_search"
android:icon="#android:drawable/ic_dialog_email"
android:title="#string/hello_world"
android:showAsAction="always|collapseActionView"
/>
<item android:id="#+id/action_compose"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
<item android:id="#+id/action_compose2"
android:icon="#android:drawable/btn_star"
android:title="#string/hello_world"
android:showAsAction="never"
/>
</menu>
This my activity:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar a = getActionBar();
a.setTitle("Mariano");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
MenuItem menuItem = menu.findItem(R.id.action_search);
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
#Override
public boolean onMenuItemActionExpand(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean onMenuItemActionCollapse(MenuItem arg0) {
// TODO Auto-generated method stub
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return false;
}
}
If I understood correctly you want to open an Android Popup Menu by click on a Button. Then all you have to do is to add this to your MainActivity:
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Creating the instance of PopupMenu
PopupMenu popup = new PopupMenu(MainActivity.this, button1);
//Inflating the Popup using xml file
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
//registering popup with OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int i = item.getItemId();
if (i == R.id.id1) {
//do something
return true;
}
else if (i == R.id.id2){
//do something
}
else if (i == R.id.id3) {
//do something
return true;
}
else if (i == R.id.id4) {
//do something
return true;
}else {
return onMenuItemClick(item);
}
});
popup.show();//showing popup menu
}
});//closing the setOnClickListener method
}
I am working on an timetable app, but I have a strange problem, I created a popupmenu that opens upon clicking an action-bar item.
The popup works, but it opens inside the action bar, I want that it opens in the view below.
My code..
#Override
public void onCreateOptionsMenu(
Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.lists_choice_mode_mulitplue, menu);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.inverse:
showPopupMenu(this.getView());
return true;
}
return false;
}
private void showPopupMenu(View v){
final Activity activity = getSupportActivity();
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(activity,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
My .xml layout files
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="#+id/group_popupmenu">
<item android:id="#+id/menu1"
android:title="Popup menu item 1"/>
<item android:id="#+id/menu2"
android:title="Popup menu item 2"/>
<item android:id="#+id/menu3"
android:title="Popup menu item 3"/>
</group>
</menu>
My action bar button .xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/inverse"
android:showAsAction="always|withText"
android:title="Week"
android:titleCondensed="Week" />
</menu>
Yeah it's fixed now!
Its wrong to to showPopupMenu(this.getView());
It should be the id of icon in action bar.. as following..
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.inverse:
showPopupMenu(R.id.inverse);
return true;
}
return false;
}
change showpopupmenu(view v) in
private void showPopupMenu(int id){
final Activity activity = getSupportActivity();
View v = activity.findViewById(id);
PopupMenu popupMenu = new PopupMenu(activity, v);
popupMenu.getMenuInflater().inflate(R.menu.popup, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(activity,
item.toString(),
Toast.LENGTH_LONG).show();
return true;
}
});
popupMenu.show();
}
It works now! Thanks for the answers it didn't help me but it is appreciated!
You are inflating your menu layout, and I think that you want to inflate your activity layout.
check this previous question that shows how to inflate a layout:
How to inflate one view with a layout