I have my MainActivity that contains a navigation drawer with items. When I click on an item,it opens up a new activity. This activity has its own layout file. I want to call the navigation drawer in this activity as well. How do I do that? Because I want to navigate through the app using the navigation drawer,rather than pressing back button all the time.
Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a DrawerLayout with the appropriate list view to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class: NavigationDrawerSetup.java. That way my activities can use the same custom adapter, etc.
Complete Reference : NavigationDrawer with Multiple Acivities
Related
I have a problem...I am developing an app in android studio.In the meantime,there is already a navigation drawer in it.But the problem is, it only appears in the home screen.When it goes to another screen, the navigation drawer is not shown. I am new to android studio but i know some of Java languages.Can anyone help me with providing anything that would help me make the navigation drawer show in all of slides and pages in my app.
from docs
Navigation Drawer
The navigation drawer is a panel that displays the app’s main
navigation options on the left edge of the screen. It is hidden most
of the time, but is revealed when the user swipes a finger from the
left edge of the screen or, while at the top level of the app, the
user touches the app icon in the action bar.
for achieve what you asked you have to use Fragments
Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities. You
can think of a fragment as a modular section of an activity, which has
its own lifecycle, receives its own input events, and which you can
add or remove while the activity is running (sort of like a "sub
activity" that you can reuse in different activities).
see this examples for more details
1. Navigation Drawer - android hive
2. Navigation Drawer exp 2
3. Navigation Drawer exp 3
Refer this answer also
Answer
You have to add fragments in that activity where the navigation drawer exist. Whenver the user will click on the option in the navigation drawer the view should be changed by replacing with the required fragment.So by using the fragments user will stay on the same activity but just the views will be changed in that activity. you can refer to the fragments documentation provided by android developers. https://developer.android.com/guide/components/fragments.html
I have an activity that extends fragmentActivity that has a navigation Drawer, and three different fragments.
Is there a way to make a navigation Drawer that has different buttons inside each of my fragments?
For example when you enter fragment 1 you will see certain buttons in the navigation Drawer, and when you go to fragment 2 you will see different buttons in the navigation Drawer.
You should just define which options are to be displayed in the NavigationDrawer depending on which current Fragment you're on, using a switch statement or if statement.
Check out this link:
How to change fragments using Android navigation drawer
Also, from this link https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
As per the Android Design guide, any drawers positioned to the
left/start should always contain content for navigating around the
application, whereas any drawers positioned to the right/end should
always contain actions to take on the current content. This preserves
the same navigation left, actions right structure present in the
Action Bar and elsewhere.
So this type of thing seems like it was planned for when the NavigationDrawer was designed.
I'm working on my App about 6 Months.
I don't know if I misread it but, is it possible to create and add a Navigation Drawer when I have different Activities?
How my App works:
SplashScreen -> LogInScreen -> Activity 1(ListView) -> Activity 2 (after click on ListView item) -> ... and so on.
Is it possible to add now a Navigation Drawer? I read something that you can add a Navigation Drawer only if you don't switch between Activities is that right?
I want a Navigation that looks on every Activity different. Is it possible or it can only have one layout?
I hope my problem is clearly explained.
Kind Regards
All your other Activities should extend The MainActivity which contains the navigationView .
helphuf links : Same Navigation Drawer in different Activities , Android: Navigation-Drawer on all activities
Yes, it is possible. I suggest following option to use navigation drawer in all Activities of App.
You have a two option :
1) Use Fragment instead of Activity and put navigation drawer to First Activity.
for Example : http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
2) Create One layout for navigation-drawer and include it to all layout of your Activities using "" tag in xml Like this link : navigation drawer layout with include layout
for Example : Same Navigation Drawer in different Activities
I have an AppCompatActivity with DrawerLayout and a NavigationView. I can start three different fragments from the NavigationView.
I want to show the checked status in the navigation drawer according to the fragment I am in.
It works fine, when navigating through the navigation drawer. But I don't know, how to achieve that also with the back button.
Probably I need to use something like
navigationView.getMenu().getItem(0).setChecked(true);
but how do I get ahold of the NavigationView from within the fragment?
Or, other way around, I would need to handle that in the main activity. Then I must need to know, which fragment is active at the moment.
I have made a nice app with a whole bunch of activities , then i needed a navigation drawer and found out that you need to have only one activity for the whole app and the individual different screens should be fragments that are inserted at runtime.
my question is :
How to convert the entire app to use fragments instead of activities ? (eg: how to preserve activity hierarchy , show a main activity when the user opens the app , different actionbar for each screen , etc...)
There is no magic converter, you need to convert manually each activity to be extended from Fragment
and add few must methods like onCreateView for the fragment instead of SetContentView of activity.
Regarding the actionbars, it sits on the Main Activity so you need to create callback events from each fragment to the main activity in order to control the action bar.
Navigation drawer has nothing related to fragments.
If you wish you can put it into activities also.
What I created was a BaseActivity with layout having navigation drawer and all other activities extend BaseActivity so that each of your activity will have drawer. Only you need to change content page for particular activity.
happy coding.