I have google map's activity, and a button that I want to inflate a popup menu.
This is the class:
public class NewMissionMapMainUser extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener, OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleMap.OnMyLocationButtonClickListener {
And this is the onClick method that is called when my button is pushed:
public void showPopup(View v) {
PopupMenu popup = new PopupMenu(this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.popup_menu, popup.getMenu());
popup.show();
}
And that is the Code that is not being called.
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item_comedy:
Toast.makeText(this, "Comedy Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item_movies:
Toast.makeText(this, "Movies Clicked", Toast.LENGTH_SHORT).show();
return true;
case R.id.item_music:
Toast.makeText(this, "Music Clicked", Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this, "Yo", Toast.LENGTH_SHORT).show();
return false;
}
}
I've tried this link:
(Deprecated) Fragment onOptionsItemSelected not being called
and setHasOptionsMenu(true);
But i've changed the fragment to extent AppCompatActivity in order the action bar to be visible, so the line "setHasOptionsMenu(true);" can't be resolved.
Does someone knows how to fix this ? Thanks
Since you're instantiating a PopupMenu dynamically, you need to provide it with the listener that will receive the click events. You should call popup.setOnMenuItemClickListener(this), assuming this is your Activity or Fragment.
Related
I have a class AddPhotoMenu
public class AddPhotoMenu extends PopupMenu implements PopupMenu.OnMenuItemClickListener {
public AddPhotoMenu(Context context, View anchor) {
super(context, anchor);
}
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
break;
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
break;
}
return false;
}
}
And I create a new instance of it however the method onMenuItemClick is never called when I click on the menu items.
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.show();
What is causing the issue?
Edit:__________________________________________________
XML of menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/popup_menu_fragevent_takephoto"
android:title="Take a photo"/>
<item
android:id="#+id/popup_menu_fragevent_selectphotos"
android:title="Select photos"/>
</menu>
Try like this
#Override
public boolean onMenuItemClick(MenuItem item) {
Log.d(TAG, "onMenuItemClick: Called");
switch (item.getItemId()) {
case R.id.popup_menu_fragevent_takephoto:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Take photo");
return true; // return true instead of break
case R.id.popup_menu_fragevent_selectphotos:
//Inflate a layout
Log.d(TAG, "onMenuItemClick: Select photo");
return true; // return true instead of break
}
return false;
}
UPDATE:
You have to set MenuItemClickListener to your popup menu like below.
AddPhotoMenu addPhotoMenu = new AddPhotoMenu(this, mAddPhotosButton1);
addPhotoMenu.inflate(R.menu.popup_menu_fragevent_addphotos);
addPhotoMenu.setOnMenuItemClickListener(addPhotoMenu); // add this line
addPhotoMenu.show();
You PopupMenu.OnMenuItemClickListener should be implemented in the Activity or Fragment where you instantiate your PopupMen.
After that addPhotoMenu.setOnMenuItemClickListener(this) should be called, where "this" represents your Fragment or Activity.
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;
}
public class MainActivity extends AppCompatActivity {
private final int item_4 = 4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onPrepareOptionsMenu();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.menu_main, menu);
menu.add(Menu.NONE, item_4, Menu.NONE, "Show");
menu.add(Menu.NONE, item_4, Menu.NONE, "Hide");
menu.add("Reset");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "You Pressed Settings", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "You Pressed Create", Toast.LENGTH_SHORT).show();
return true;
case R.id.item3:
Toast.makeText(this, "You Pressed Delete", Toast.LENGTH_SHORT).show();
return true;
case item_4:
Toast.makeText(this, "You Pressed Hide", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem mii = menu.findItem(R.id.item2);
mii.setEnabled(false);
return super.onPrepareOptionsMenu(menu);
}
}
In the above code I have created a menu item list for the app bar and I have created a button in my app and i want some of my items to be disabled after button click but I am not able to do so as I am not gettig how to call onPrepareOptionsMenu() on button click? Can anyone suggest a solution for how to disable the menu items after button click?
I will show short example:
Only one note for this method: don’t forget to clear menu items (menu.clear();) on every method start.
Example:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* Class which shows how to change dynamically options menu items
*/
public class Main extends Activity {
private Button clickBtn;
private boolean isChangedStat = false;
private static final int MENUITEM = Menu.FIRST;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
clickBtn = (Button) findViewById(R.id.click);
clickBtn.setText("Click me");
clickBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(isChangedStat) {
isChangedStat = false;
} else {
isChangedStat = true;
}
}
});
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear();
if(isChangedStat) {
menu.add(0, MENUITEM, 0, "True");
} else {
menu.add(0, MENUITEM, 0, "False");
}
return super.onPrepareOptionsMenu(menu);
}
}
Reference from:
Android: How to enable/disable option menu item on button click?
Once the activity is created, the onCreateOptionsMenu() method is
called only once, as described above. The system keeps and re-uses the
Menu you define in this method until your activity is destroyed. If
you want to change the Options Menu any time after it's first created,
you must override the onPrepareOptionsMenu() method. This passes you
the Menu object as it currently exists. This is useful if you'd like
to remove, add, disable, or enable menu items depending on the current
state of your application.
you could call the method invalidateOptionsMenu(); in your button's click listener. It will call onPrepareOptionsMenu() again, all your logic to disable could be written here.
Anyway, documents covers all.
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 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