Check if popup menu become inactive / check if popup menu is visible - java

Hello I am making a small app where i have a list and different popup menus.
I am shading the selected item when the popup menu is triggered.
Is there a way to check if the popup menu that has been active is now not active any more?
For instance I have the popup menu open and i press somewhere else on the screen. The popup menu Disappears. Is there a listener to capture that?
popupMenu2 = new PopupMenu(getContext(), v);
popupMenu2.getMenuInflater().inflate(R.menu.popup_unknown_number,
popupMenu2.getMenu());
popupMenu2.setOnMenuItemClickListener(new
PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.delete_unknown) deleteVM(v,model);
return true;
}
});
popupMenu2.show();
Shading is done by
v.setSelected(true);

Found he answer. There is a dismiss listener for PopupMenus
popupMenu.setOnDismissListener(new PopupMenu.OnDismissListener(){
#Override
public void onDismiss(PopupMenu popupMenu) {
// TODO Auto-generated method stub
}
});

Related

How can I fix the problem that a MenuItem takes on the ID of a previous one

At the beginning I have two items and the 2nd gets a listener with:
final View view = findViewById(R.id.list_item);
if (view != null) {
view.setOnLongClickListener(v -> {
Toast.makeText(getApplicationContext(), "Long pressed", Toast.LENGTH_SHORT).show();
return true;
});
}
}
But if I add a new item with menu.add("name"); the listener is on the newly added item and not on the old one (list_item)
I have already tested many other alternatives, but they don't work!
I just need help on how to keep the item ID or how the listener stays on the item.
When you add an item using ``menu.add(...)` it returns a reference to the newly added item. You can set the listener like this:
final MenuItem newItem = menu.add("New item");
newItem.setOnMenuItemClickListener(menuItem -> {
// Handle your click here!
return true;
});
However, you should add/remove menu items by overriding onPrepareOptionsMenu(Menu menu). When you use menu.add(...) use the overload that allows you to set the itemId:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.add(Menu.NONE, 314159, Menu.NONE, "New item");
return super.onPrepareOptionsMenu(menu);
}
Note that the above will be called each time the menu is going to be shown (very convenient).
Then you can handle the menu being selected from onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == 314159) {
// Handle menu selection here!
}
return super.onOptionsItemSelected(item);
}
Check the AppBar/Menu guide here: https://developer.android.com/training/appbar

How to Recognise which menuItem is disabled?

Heyy,
I have implemented menu, where there are two menu items and when I click on first item, the other menu item goes disabled and vise-versa.
And when I click on back button navigation icon, I have to check if first item is disabled or not and if disabled, then turn the second item enabled, and if not then onBackPressed();
So, I don't know how to recognize which item is disabled.
Please help me fast.
There are some code references
This is my current try and it also have some errors. Please help me find another way or fix this code.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (menu.getItem(0).getActionView().getVisibility() == View.VISIBLE) {
onBackPressed();
} else {
menu.getItem(1).setEnabled(false);
menu.getItem(1).setVisible(false);
menu.getItem(0).setEnabled(true);
menu.getItem(0).setVisible(true);
}
}
});
in onCreateOptionsMenu() store the menu into a local class field, and then to check if a certain menu item is enabled/disabled use isEnabled()
MenuItem item = menu.findItem(R.id.item_id);
if (item.isEnabled()) {
// enabled
} else {
// not enabled
}
Or you can use the order of the item among the menu items to get a particular item
MenuItem item = menu.getItem(0);

How to fix Context Menu Bar functions (copy/paste etc) after calling setCustomSelectionActionModeCallback() on EditText?

I'm writing Notepad App in which I've got slider menu showing some text format panel. I toggle view of this panel when user tries to select some text, so I've implemented my menu-toggling code into my EditText's setCustomSelectionActionModeCallback() which looks like this:
private void manageContextMenuBar(EditText editText) {
editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
// There menu is hidden
public void onDestroyActionMode(ActionMode mode) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.VISIBLE) {
findViewById(R.id.sliderMenu).setVisibility(View.GONE);
}
}
// There menu shows up
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
if (findViewById(R.id.sliderMenu).getVisibility() == View.GONE) {
findViewById(R.id.sliderMenu).setVisibility(View.VISIBLE);
}
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
});
}
When I long click on text my format menu shows up, and also software context menu with paste/copy/cut button on it.
The problem is that because of my "Overriding" context menu functions, they stopped working. I can click the buttons, but they doesn't work.
I hope You will understand my problem
Any help will be appreciated :)
You should return false from onActionItemClicked method. This way when you click on those menu items Android uses the default actions.
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
From the onActionItemClicked method Documentation:
Returns: true if this callback handled the event, false if the standard MenuItem invocation should continue.

Popup menu appears blank

I am working on an app, in which I am want to use a pop-up menu to control some actions and settings. However, when I launch the app in my emulator, the items appear blank, although when I click on them, the action is fired and it works. Here is a screen of the emulator:
I am following the guide, so my code does not differ much from the dev guides, but here is my code:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long ID)
{
// TODO Auto-generated method stub
courseId = ID;
fields = sqldbase.query(DBHelper.courseTable, new String[] {
DBHelper.courseID, DBHelper.courseName,
DBHelper.courseProf, DBHelper.averageGrade },
DBHelper.courseID + " = " + ID, null, null, null, null);
PopupMenu popup = new PopupMenu(getBaseContext(), view);
popup.getMenuInflater().inflate(R.menu.courses_popup_menu,
popup.getMenu());
popup.show();
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
{
#Override
public boolean onMenuItemClick(MenuItem item)
{
Log.i(TAG,"OnMenuItemClick Fired"); return false;
}
});
return false;
}
});
Could this be related to a problem with themes? I have been trying to use a Holo.light.NoTitleBar theme, so in my xml in eclipse, the word Passbook does not show, and everything is white, instead of black. However, after running in the emulator, the theme is ignored, and this shows up.
Thanks.
fixed it. apparently, popup menus use the attribute android:title instead of android:text.

Action Bar Home Button not functional with nested PreferenceScreen

I found a workaround to actually enable the ActionBar home button on the nested PreferenceScreen... however it doesn't call OnOptionsItemSelected in my PreferenceActivity. Anyone know a way to actually use the home button on a nested PreferenceScreen?
Modification of post 35 here:
http://code.google.com/p/android/issues/detail?id=4611
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference)
{
super.onPreferenceTreeClick(preferenceScreen, preference);
if (preference!=null)
if (preference instanceof PreferenceScreen)
if (((PreferenceScreen)preference).getDialog()!=null)
((PreferenceScreen)preference).getDialog().getActionBar().setHomeButtonEnabled(true);
return false;
}
I had this problem recently and this is how I solved it. Firstly to access the PreferenceScreen I use the exact same method you mentioned above.
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);
// If the user has clicked on a preference screen, set up the action bar
if (preference instanceof PreferenceScreen) {
initializeActionBar((PreferenceScreen) preference);
}
return false;
}
From here I looked into what a PreferenceScreen is, and I was saddened to find out it is just wrapper of a Dialog. Moving forward, I then set the actionbar display options and attempt find the home button area. This unfortunately wasn't too easy to get, but with the help of the hierarchy viewer I managed to gain access by finding the home icon and then its parent views. Once we have access to the containing LinearLayout, we can attach an onClickListener where we dismiss the PreferenceScreen's dialog, which calls PreferenceScreen's onDismissListener and returns us to the previous screen.
/** Sets up the action bar for an {#link PreferenceScreen} */
public static void initializeActionBar(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();
if (dialog != null) {
// Inialize the action bar
dialog.getActionBar().setDisplayHomeAsUpEnabled(true);
// Apply custom home button area click listener to close the PreferenceScreen because PreferenceScreens are dialogs which swallow
// events instead of passing to the activity
// Related Issue: https://code.google.com/p/android/issues/detail?id=4611
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null) {
OnClickListener dismissDialogClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
}
}

Categories