I have previously used Android Studio's wizard to implement the Master-Detail pattern, and tried to convert the Detail to have a drawer instead of the "up" button.
I went in AndroidManifest.xml and removed the "parent" attribute on the Detail's activity settings, but it still didn't work.
Then I realized that in my Detail Activity's onOptionsItemSelected function, I had forgotten to delete this:
if (id == android.R.id.home) {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpTo(this, new Intent(this, SongListActivity.class));
return true;
Now it sort-of works, but I have to click the navigation drawer icon 2 times. This is the "navigation drawer icon" I'm referring to:
I noticed that when I click on an item in the Master list, it launches the Detail activity, and it seems to not be in focus. The Navigation bar is not visible at first. This is what I mean by saying "Navigation Bar":
When I click the Navigation Drawer Icon once, the Navigation bar shows up. When I click it again, then the drawer will open.
How do I get it so the new activity is already in focus as soon as it's launched, so I only have to click the Navigation Drawer Icon one time?
Edit: I noticed if I try to click anything in the Action Bar, it takes 2 clicks. I tried several other apps on my Android and they don't have this problem. Usually, they start out with the Navigation Bar already there. Maybe I have some code hiding the Navigation Bar, making the Action Bar not be in focus?
I just looked through my code, and realized that I had forgotten to delete rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); from before. It was in my onCreateView method of my DetailFragment class. I commented it out, and the problem was immediately fixed.
Related
When you select an item in the Bottom Navigation Bar provided by Android Studio it displays a fragment. I want to make a logout item that when you select it, it calls a method but don't actually bring me a fragment.
I understand that NavigationUI set up all the configuration of the navigation bar, so when I try to implement navController.addOnDestinationChangedListener() and add my action to my logout item, I think that overrides the nav configuration and I just can't navigate over the items
Does I have to configure by myself and add a listener on every item?
I also want to make that when a confirmation dialog appears when you clicked the logout item, if u select cancel it returns you to the previous state (EX: I'm in Home fragment, if I click on logout item, and I cancel it, it returns me to the Home fragment)
You can enable/disable any menuitem in NavigationView...
NavigationView navigationView= findViewById(R.id.nav_id_in_layout)
Menu menuNav=navigationView.getMenu();
MenuItem nav_item2 = menuNav.findItem(R.id.nav_item2);
nav_item2.setEnabled(false)
When using a NavigationView with a DrawLayout and an ActionBar, jetpack provides some powerful convenience methods to hook everything up together so that these items are easier to implement.
The method setupActionBarWithNavController provided in the NavigationUi library is an extension method for activities that adds a hamburger button to open your drawer and if you override the onSupportNavigateUp:Boolean method on your activity to call navigateUp(drawLayout:DrawLayout, navController:NavController) method, that method will change the hamburger button to a back button and back automatically with a fancy animation, which is super cool.
However it seems that this method is implemented as follows:
public static boolean navigateUp(#Nullable DrawerLayout drawerLayout,
#NonNull NavController navController) {
if (drawerLayout != null && navController.getCurrentDestination().getId()
== navController.getGraph().getStartDestination()) {
drawerLayout.openDrawer(GravityCompat.START);
return true;
} else {
return navController.navigateUp();
}
}
As you can see here, this method has the basic logic of:
if you are not at the start destination of the navgraph, then the button is a back button, otherwise its a hamburger button
This means that only the start destination can open the draw menu via an actionbar button, while all the other destinations have a back button instead, and must swipe to open the draw menu.
Why is this the case? It seems this is a conscious design decision by the android team. Is it frowned upon to have a hamburger button available on multiple destinations?
I would like to have a few main branches that have a hamburger menu and all the screens that branch of from those to have a back button. Is there a way to implement multiple NavGraphs and link them together in one NavigationView?
u_u
I reported a problem I had with the use of a Toolbar inside a fragment and the NavigationUI helper in bug 109868820.
The Googler that helped me indicated clearly (article #7):
[...] the discussions with the material design team has made it clear that the navigation drawer is a global navigation pattern that should be available everywhere
So the material design drawer has to be available on every screen, including the deeper ones when the navigation button is the "up" arrow (not the hamburger). In which case, the drawer is only available from a swipe gesture, as the navigation button navigates up in the app stack.
Indeed (#4 in bug):
You'll still get the title set from the android:label in your navigation graph and proper behavior when it comes to the Up button (particularly important if you're using the DrawerLayout versions)
So the behavior that you found in the source is indeed the "proper" behavior.
Hence:
top level destination: drawer opened through swipe and hamburger button
non-top level destination: drawer opened through swipe only, button navigates up (no hamburger)
In other words, the hamburger button should only be used on top-level destinations.
I have implemented a sliding menu navigation in my app like in this picture.
When the user clicks an item in the sliding menu, a Fragment is created for the corresponding menu entry and it replaces any existing fragment. When the used navigates further away, for example, the top level Fragment is a ThingListFragment for a list of thigs, and he taps one to open a ThingDetailFragment, then I want to replace the sliding menu icon with an icon of a back arrow (so the user must first go back to the top level either by this button or by hardware back button before being able to access the menu).
At this moment I'm setting this manually and it's time consuming and error-prone if I have a lot of fragments like this.
Is there some clever way to achieve this?
Try to make a basic fragment class (for exaple: FragmentBasic) where you register your action bar icons, and then every fragment who needs these icons, extends your FragmentBasic class. after this just implement the extra layout you want
I have a Activity A, a ListFragment P and 2 Fragments Q and R.
When the app is launched, A is created, which loads P. Based on what user clicks, it is replaced by Q or R.
Now by referencing this tutorial, I have implemented a Navigation Drawer which shows certain items to the user. However, since I have implemented the Navigation Drawer in the Activity, it shows for all the Fragments.
I want it to be only available to P.
(Very much similar to Googles Gmail app. When the user is on the main screen - the drawer is present. When user taps to open an email, the drawer changes to back button)
I am not sure how to translate the above code. Any help is appreciated.
I solved this issue by simply overriding the up carat behavior by calling the mDrawerToggle.setDrawerIndicatorEnabled(enable) and passing the boolean enable or disable as needed.
(The fragments where I didn't want the drawer to show called this method with false and where I wanted the drawer to be shown called this method with true. I put the call inside the onResume() of the respective fragment for obvious reasons.)
This works exactly like I want, and I did not have to change the design of my project :).
What you can do is create a new FragmanetActivity S and replace Q and R accordingly. If you are app is for Android 3.0 lower user ActionBarActivity create a actionbar and set its setDisplayHomeAsUpEnabled(true). As the new FragmentActivity will have new layout there will be no NavigationDrawer.
As in new action bars, when user clicks on any activity on dashboard, app will take user to new activity, at the same time along with home icon in action bar there would be an arrow showing this activity been started from other activity. How to get this feature with Sherlock android bar?
home icon in action bar there would be an arrow showing this activity been started from other activity
That is not what that arrow means. That arrow means that you are giving the user an option of moving "up" a navigation hierarchy, in addition to the BACK button offering navigation back to the preceding activity. This is described in greater detail in the Android Design documentation.
How to get this feature with Sherlock android bar?
The same way you do with the native action bar: call setDisplayHomeAsUpEnabled(true) on the ActionBar.