ActionBar under notification/title bar - android

I'm having an issue with the ActionBar in my app; In certain scenarios the ActionBar appears to go "under" the notifications/title bar. It's reproducible each time and i can't figure out why it's happening. I use the ZXING application with Intents to scan barcodes and return them to my app, and it's at some point during this process the issue occurs.
I thought it'd be best to show you the issue with pictures.
1: The app home screen, all is normal.
2: Use the menu item to scan a barcode. This appears as expected.
3: The product page for the scanned item appears normal. If i click 'Cancel' however...
4: The ActionBar has now gone under the notifications/title bar.
The only other mention of a bug such of this (that i can find) is in this GitHub issue for ActionBarSherlock (which i'm using): https://github.com/JakeWharton/ActionBarSherlock/issues/602
I have checked and i'm not doing anything weird with configChanges as Jake mentions.
This issue is seen on my 4.2.2 device, i'm unable to test on a pre-ICS device unfortunately.
Any thoughts or suggestions are welcome!

I'm guessing it not getting reset back when you come back from the zxing screen. In your Activity for "Best Before", try reseting the window flags for fullscreen something like:
#Override
protected void onResume() {
super.onResume();
//getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setSystemUiVisibility(this, View.SYSTEM_UI_FLAG_VISIBLE /* SYSTEM_UI_FLAG_VISIBLE=0 */);
}
private static void setSystemUiVisibility(final Activity activity, final int newValue){
if (activity.getWindow() != null){
View v = activity.getWindow().getDecorView();
if (v != null) {
try {
Method methodSetSystemUIVisibility = v.getClass().getMethod("setSystemUiVisibility", int.class);
methodSetSystemUIVisibility.invoke(v, newValue);
} catch (Exception noop) {
}
}
}
}

Related

Android TabLayout annoying popup (tooltip) on tab title long click

I imagine someone has had this question before, I just don't quite know what the right keywords are to find the answer? I am making an android app with an activity that includes tabs using TabLayout. Nothing fancy, just really standard stuff. In fact, so far I've done literally nothing but make a completely new application with a single tabbed activity using the auto-generated code from Android Studio. Everything works fine, but there is one feature I cannot figure out how to turn off -- when I long click on any tab, a little rectangular alt text or something with the title of the tab pops up on screen just above the tab. It's not the end of the world if I can't eliminate it, I just find it to be irritating and incompatible with the overall desired feel of my app given that it's literally just duplicating the tab title. I can't find any code that is causing this to appear, so I don't know how to delete it. The picture below shows what I'm talking about circled in red.
If anyone needs me to post code to help answer, I can... but you can also just make a new tabbed activity in a throwaway application in Android Studio and get exactly the same boilerplate code I have.
Edit: I added the term "tooltip" to the title so others can find the relevant thread more easily if they have the same problem.
Kudos to Mike M. for the answer, shown in comments above. I implemented it successfully, so if anyone comes back here looking for the answer, here's the successful java code, which is placed in the onCreate() method of the activity containing the tabLayout:
// turn off that tooltip text thing immediately on activity creation
for (int i=0; i<tabs.getTabCount(); i++) {
TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
}
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition(); // syntactic sugar
viewPager2.setCurrentItem(tabPosition, true);
// Repeat of the code above -- tooltips reset themselves after any tab relayout, so I
// have to constantly keep turning them off again.
for (int i=0; i<tabs.getTabCount(); i++) {
TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

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

Android KitKat 4.4 TalkBack view refresh issue

I'm running my app on an Android 4.4.2 (KitKat) device.
My app has a ListView.
When I add the last item to the list view, it shows some content, and about 5 seconds later, it changes (loads a different view to that specific list item).
While TalkBack is on -
If, in that 5 seconds window, I click on the last item - the TalkBack marks it, reads it, and does not let the view change.
I do not have this issue on previous Android version.
Anyone knows why this happens? and if I can override this behavior?
Thanks!
PB
I did not find a direct solution.
I have solved the issue by overriding accessibility default behavior.
I removed the mark around the view and kept only the reading part.
This is the code I used:
view.setAccessibilityDelegate(new AccessibilityDelegate() {
#Override
public boolean performAccessibilityAction(View host, int action, Bundle args) {
//call super to perform the action
boolean ret = super.performAccessibilityAction(host, action, args);
//call super with remove-focus action to remove the mark around the view
super.performAccessibilityAction(host, AccessibilityNodeInfo.ACTION_CLEAR_ACCESSIBILITY_FOCUS, args);
return ret;
}
});

How to always show Android settings button on ActionBar?

How can I always show Android settings button on ActionBar? (4.0+)
I would like to show it even if the device has an hardware button for settings, so it would be same with devices with and without hardware buttons.
This is what I'm talking about: http://oi48.tinypic.com/2j104l0.jpg
There is one awful hack which generally have been answered on many questions. Call this from your onCreate():
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
It is highly suggested not to use this hack as sHasPermanentMenuKey field can change anytime. However this has been working for me and others uptil now. See
Android action bar not showing overflow
On a second note, the hardware button is present on the phone for a reason. Just showing the addition overflow menu might confuse your users. Ideally a user would be very much accustomed to using his/her personal phone. So, if the user's phone has a hardware button for overflow menu option, then it need not have the icon on the top of action bar. Having an addition button might confuse users due to difference in behavior in different apps.
Hope that helps.
Edit:
In short, its not recommended as sHasPermanentMenuKey field in the Android code can be changed anytime, which will break your app if not found.

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

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);
}

Categories

Resources