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
I've been looking around a lot lately but haven't really found much on this.
I'm making my third Android app and I'm looking to implement an intro screen on first run of the app where a series of images are shown explaining the apps functionality and the idea behind it; you can swipe the images left or right and at the last image you get to the app by swiping.
I really like the sort of thing they have done with the CamScanner app but despite my searching I have no idea how to implement it other knowing a little bit about some people referring to Fragments. Any help would be appreciated greatly and since we need better UI on Android, a good answer would help a lot of developers take the cue! :)
create a method to show popup window. show images in a scroll view in that popup. at last image set touch listener to dismiss that popup.
and call that method from onResume method of your Activity like this
protected void onResume(){
super.onResume();
SharedPreferences pref = getSharedPreferences(MyPrefs, MODE_PRIVATE);
boolean b = pref.getBoolean("FirstTime",true);
if(b)
{ new Handler().postDelayed(new Runnable() {
public void run() {
showIntroPopup();
}
}, 100);
}
}
in that popup set "FirstTime" boolean to false in SharedPreferences.
Can someone explain how to get this to work please. Says its deprecated, just wondering if I can force it or if there is another way to change my title label when the SlidingDrawer is opened then change it back when it is closed. I still have a lot to learn so if possible please reply with something that is cut and paste friendly ;) Thanks.
Jesse
OnDrawerCloseListener onClick_DrawerClosed = new OnDrawerCloseListener() {
#Override
public void onDrawerClosed() {
titletext.setText("Flow Charts");
}
};
OnDrawerOpenListener onClick_DrawerOpened = new OnDrawerOpenListener() {
#Override
public void onDrawerOpened() {
titletext.setText("Options Menu");
}
};
SlidingDrawer in general is deprecated as of API 17 (the latest). This doesn't mean you can't use SlidingDrawer and it's methods, but that it's "frowned upon" and Eclipse will give you warnings. You can make those go away by right clicking on the warnings and selecting "Suppress..."
Look here for a more thorough discussion:
SlidingDrawer deprecated
What I need unless my tablet is connected to a server I cant have the tabs being changed. I have a tab host that is all run off of the same java file. I figure all I need to do is have some sort of a test in the file to say unless boolean is true dont let the code change the tab. However I dont know how to do this? If you need to see any of my code just leave that in the comment box. Thanks for all your help !
Well I guess you can try disabling/enabling clicks on the tabs by calling:
myTabHost.getTabWidget().setClickable(isConnectedToServer);
But I'm not sure that's good UX, how about letting your users change tabs, but if the content can't be reached displaying a message inside the main view of the tab "server unreachable, check your internet connection" or something like that.
UPDATE:
Try this instead (for each of your tabs):
myTabHost.getTabWidget().getChildTabViewAt(0).setEnabled(false);
myTabHost.getTabWidget().getChildTabViewAt(1).setEnabled(false);
myTabHost.getTabWidget().getChildTabViewAt(2).setEnabled(false);
ANOTHER UPDATE:
public class MyActivity extends Activity {
private TabWidget mTabWidget = null;
protected void onCreate(Bundle savedInstanceState) {
...
mTabWidget = myTabHost.getTabWidget();
...
}
protected void refreshTabs(boolean isConnected) {
mTabWidget.getChildTabViewAt(0).setEnabled(isConnected);
mTabWidget.getChildTabViewAt(1).setEnabled(isConnected);
mTabWidget.getChildTabViewAt(2).setEnabled(isConnected);
}
Now you can call refreshTabs whenever you want in your code to make them enabled/disabled.
I am making an app that has a scrolling screen like the homescreen style. I have implemented this solution:
Android Homescreen
It works great but I also want there to be buttons on each page that you can click to go to the next page but I just can't figure out how to do it! can someone help? I've been staring at this code for days now!
Thanks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
UPDATE - HELP
I really don't understand how to get around the problem of calling the SetToScreen from the other activity, Can anyone help as if I try I do keep getting Static call errors.
Look at
public void setToScreen(int whichScreen) {}
Use this function to set to a screen on a click.
you should extend Draggablespace by adding a function to get the current space like:
public int getCurrentScreen() {
return this.mCurrentScreen;
}
then you can write your own functions in your activity like
public void nextScreen() {
draggableSpace.setToScreen(draggableSpace.getCurrentScreen() + 1));
}
The same for previous screen.
Now you only need to check if there is an additional screen waiting if you are going forward or backward.
(Of course draggableSpace is your object of the class draggablespace...not a static call!)