Android context sub-menu opened-closed-reopened by itself - android

I have a context menu which include a sub-menu, when-ever I tap on the item to open the sub-menu, the sub-menu opens/closes and reopens quickly. That's very annoying but more problematic some of my users don't see the sub-menu at all, it opens/closes and that's it!
Now after experimenting I figured out that long-pressing the item actually works as soon as I release the item: the sub-menu opens properly and stays open!
So I decided to build a very basic project believing I had a bug in my app, created a new app with the wizard, a single activity, a single text on which I registerForContextMenu and a context menu with a simple sub-menu.
The issue reproduced itself immediately!!! Does anyone experience the same issue and could tell me what am I doing wrong? I believe I followed documentation and samples, but I can't find any information on this problem anywhere!!!
I've posted the issue on Google groups and reported as an issue on Android project, but so far no-one responded, here are the links to both which include the test project (not sure how I can attach a file here?).
https://code.google.com/p/android/issues/detail?id=53239&can=4&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/SLteohmgyy0

To solve this, I had to get rid of any sub-menu in context menu and instead open another context menu on item selection.
The following got rid of the flickering and ensured the sub-menu remained open. Had to use a spare hidden view to open the new context menu though.
if (id == R.id.menu_item_for_sub_menu)
{
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
View v = vg.findViewById(R.id.fake_view_for_context);
if (v != null)
{
registerForContextMenu(v);
openContextMenu(v);
unregisterForContextMenu(v);
}
}
}, 0);
}

Related

How to get ActionMode.Menu working properly in OnActionModeStarted on Android Xamarin Forms?

I have the following code to introduce menu items into the system context menu upon text selection on a Label.
public override void OnActionModeStarted(ActionMode mode)
{
IMenu menu = mode.Menu;
menu.Add("MItem1");
menu.Add("MItem2");
menu.Add("MItem3");
menu.GetItem(0).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 0));
menu.GetItem(1).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 1));
menu.GetItem(2).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 2));
//test code -> this works fine
menu.Add(0, 999, 0, "test");
//item is found, item.IsEnabled == true, item.IsVisible == true
IMenuItem item = menu.FindItem(999);
base.OnActionModeStarted(mode);
}
It works fine on a Lenovo device and was previously working on a Samsung device, but over time due to, I suspect, one or two Samsung system updates, the method no longer has any effect.
I've run the code through the debugger and the code can be stepped through line by line, but the system menu is completed unaffected by the added menuitems and continues as if my code hasn't been called at all.
Any ideas?
I have both a workaround and a solution.
Workaround
I added:
mode.Hide(1);
to the above code. It helps refresh the menu and the correct menu items appear.
Solution
I did another Samsung OS upgrade and the problem has disappeared. Seems like it was an OS problem after all.

NavigationView menu does not update

I am writing here about an issue that was introduced when we migrated from the AppCompat library to the AndroidX library. While doing so, we switched from android.support.design.widget.NavigationView to com.google.android.material.navigation.NavigationView and that’s when the following issue started.
In our NavigationView design, in order to save space, we implemented an expandable menu, so that when users clicks on the “more” button, the menu expands to show more options. It starts off with only some options visible, and the rest are not visible, as follows;
Option 1
Option 2
More…
Upon clicking on the “More...” button, the menu expands to;
Option 1
Option 2
Option 3
Option 4
Option 5
Option 6
To do this we used following code;
#Override
public boolean onNavigationItemSelected(MenuItem item)
{
....
if (item.getItemId() == R.id.nav_more)
{
item.setVisible(false); // hide the “More” item
getMenu().findItem(R.id.nav_option_3).setVisible(true);
getMenu().findItem(R.id.nav_option_4).setVisible(true);
getMenu().findItem(R.id.nav_option_5).setVisible(true);
getMenu().findItem(R.id.nav_option_6).setVisible(true);
return true;
}
.......
return false;
}
Well, this code has worked in the past, but when we migrated to using the androidx library, poof, it stopped working. Well, it did work a bit. The “More...” button got hidden, but the previously hidden options, were not being displayed.
As, it took me many hours to solve this issue, and to save others this headache, I will explain the issue and the solution.
The first thing to do in such cases, is to look at the source code. As the code is open source, I was able to get it at github. At first glance I didn’t get smarter. I found that the NavigationView has a NavigationMenuPresenter object field (called presenter), that has a method called updateMenuView() which calls adapter.update(), which calls prepareMenuItems() and notifyDataSetChanged(). This sounded like the needed fix, so using reflection, we accessed and called the updateMenuView() method, but surprisingly, it did not help!
So, I decided to take it to the extreme, and see what happens if I call getMenu().clear(), and believe it or not, nothing happened. It seems that any changes made to Menu after the NavigationView is shown, are ignored. But a quick look through source code, I could not see any reason for that.
So how do I solve this issue? I tried using the latest alpha version of the library, but I still have the same issue.
Well, after much work, I found the solution. It's actually simple. Just hold on for the answer.
Lionscribe
So I was back to the source code, searching for some clue, when I fell upon a method called setUpdateSuspended(boolean updateSuspended). Well, that sounded suspicious! I searched for usage of this method, and found it being called in the onClick callback. Here is a minimized version of the code;
#Override
public void onClick(View view) {
NavigationMenuItemView itemView = (NavigationMenuItemView) view;
setUpdateSuspended(true);
MenuItemImpl item = itemView.getItemData();
boolean result = menu.performItemAction(item, NavigationMenuPresenter.this, 0);
setUpdateSuspended(false);
}
Bingo! It seems that while handling clicks, the NavigationView suspends and will not recognize any changes done to menu. I am not sure the reason for this, but as we were updating the menu in the onNavigationItemSelected callback, which is called by the onClick method, the menu updates are ignored.
Well, once I understood the issue, the solution was simple and clean. I just wrapped the code in a Runnable, and posted it, so that it runs after the onClick method returns, and setUpdateSuspended is set back to false. Here is the updated code;
#Override
public boolean onNavigationItemSelected(MenuItem item)
{
....
if (item.getItemId() == R.id.nav_more)
{
final MenuItem itemFinal = item;
post(new Runnable()
{
#Override
public void run()
{
getMenu().findItem(R.id.nav_option_3).setVisible(true);
getMenu().findItem(R.id.nav_option_4).setVisible(true);
getMenu().findItem(R.id.nav_option_5).setVisible(true);
getMenu().findItem(R.id.nav_option_6).setVisible(true);
itemFinal.setVisible(false); // hide the “More” item
}
});
return true;
}
.......
return false;
}
Viola! The expandable menu now works like it used to, the hidden items are now being shown!
I hope this will be of help to others with same issue.
Lionscribe

Solution for OnPointerExit() Triggering on Touch Release On Android?

I have created some custom radial menu buttons for my Android game. The radial menu displays when the game object with the menu is touched. I'm using mouse events to activate the menu, which works in Unity and also when built to Android. When the menu is active, you can mouse or slide over a menu item to select it. If you then release on that menu item, it will pass the selection to the radial menu, which then takes the appropriate action.
The following works in Unity:
public void OnPointerEnter (PointerEventData eventData)
{
myMenu.selected = this;
Debug.Log ("Menu selection is now: " + this.action.ToString ());
defaultColor = circle.color;
circle.color = Color.white;
}
public void OnPointerExit (PointerEventData eventData)
{
myMenu.selected = null;
Debug.Log ("Menu selection has been nulled out.");
circle.color = defaultColor;
}
However, it does not work correctly when built to Android. Via some debug testing, I've determined that in Unity, if I activate the menu and mouse over a menu item, then release the mouse, myMenu.selected is correctly assigned. However, on Android only, lifting my finger over the menu item processes a final OnPointerExit, which nulls it out, meaning that menu never gets a proper selection. The mouse does not behave this way--it doesn't treat the pointer as having exited when the mouse button is released.
I can "solve" this problem by switching everything to touch, but then I cannot test using the Unity player and my mouse. So far, everything via mouse also worked correctly via touch. This is the first issue I've run into. Is there any clean solution for this? Or is the best solution to use macros to determine if I'm in the editor and have separate mouse and touch code for each?
It depends on the behavior you trying to implement.
Touch input does not have the notion of "hover" so you should probably be using the IPointerClickHandler interface and/or the IPointerDownHandler and IPointerUpHandler interfaces instead.
I recommend separating the hover logic vs touch/click clearly by using the preprocessor directive #if UNITY_STANDALONE around your hover-code.

removing or overriding certain items from action bar

Below is an image of the action bar on a Samsung Tab2 action bar running 4.0.3 android ICS
from left to right we have the "back, home, recent apps, screenshot, mini-app launcher, and system menu" buttons. Im certain i am not using the correct names for all these which is why i listed them from left to right along with the above image.
I know i can override the the functionality of "back" using:
#Override
public void onBackPressed() {
// Do something custom here
}
i also know that i can NOT override or remove the home button but i was wondering if i could remove or override the "recent apps", "take screen shot", and "mini app launcher"
would be nice if i could remove the back as well..
We just rolled out almost 100 GTab2s and have another 1,000 on order. This is something we've looked into extensively, especially in regards to screenshot and the minitab bloat. You can not remove the screenshot button, nor minitab, unless you root the device and modify the system image. (in short: you can't).
You could "remove" the home button by implementing your own launcher and getting rid of touchwiz.
FWIW, Samsung's B2B folks have been very helpful in supporting our efforts, even at our relatively small quantities. If you were building, say, a kiosk app around the GTab2, you might be able to get them to supply you with a less bloated image.
static public final String[] pkgs_GT_P3113_LH2 = new String[] { "com.kobobooks.samsung.android",
"com.netflix.mediaclient",
"com.nim.discovery",
"com.osp.app.signin",
"com.peel.app",
"com.samsung.mediahub",
"com.samsung.music",
"com.sec.android.app.gamehub",
"com.sec.android.app.minimode.res",
"com.sec.android.app.music",
"com.sec.android.app.readershub",
"com.sec.android.app.samsungapps",
"com.sec.android.app.sns3",
"com.sec.android.daemonapp.ap.yahoonews",
"com.sec.android.daemonapp.ap.yahoostock.stockclock",
"com.sec.android.widgetapp.ap.yahoonews",
"com.sec.android.widgetapp.at.yahoostock.stockclock",
"com.sec.android.widgetapp.minipaper",
"com.sec.chaton",
"com.sec.minimode.music",
"com.sec.pcw",
"com.tgrape.android.radar",
"com.zinio.samsung.android" };

button bar similar to twitter app for android

I want to obtain a similar tab effect with the button bar of twitter app.
I wish to click on a button and change the view down. I can switch activity but I think It's wrong, or not?
The top bar have to be fix like a frame. Like this:
Ok now I post a part of my idea (i found something similar here: http://www.talkandroid.com/android-forums/android-development-answers-tutorials-code-snippets/1515-how-i-open-image-imagebutton.html)
code:
newsButton = (ImageButton) findViewById(R.id.news);
newsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// on click go to the news view (no activity yet)
setContentView(R.layout.news);
}
});
Like in the Google IO App?
If so, the Source Code is freely available here.
Okay, a little tour on how Google does it:
The activity_home.xml-layout
includes (Line 21) the
actionbar.xml-layout (This is done
in every Layout so the Actionbar
must not always be duplicated).
The actionbar.xml-Layout
creates a LinearLayout for
the UI-Elements.
Then, for example the
HomeActivity-Activity sets
the content view to the
activity_home.xml-layout, receives
an ActivityHelper-class and calls
its setupActionBar()-method.
The mantioned ActivityHelper-class
is in the hg/ android/ src/ com/
google/ android/ apps/ iosched/
util/-package and has the
setupActionBar()-method which
creates the Action bar.
This might be easier then it looks. Read your way through the Source Code and try it yourself.
I think these controls are Radio Button/ Radio Group with customization.

Categories

Resources