How do I add NavigationDrawer from an activity template to other activities? - android

I've been playing around with the Drawer Menu activity template from Android Studio since I haven't used a drawer menu before. Here's the layout file.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<!-- Includes a Toolbar and FloatingActionButton. Generated by Android Studio-->
<include layout="#layout/app_bar_drawer_menu" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_drawer_menu"
app:menu="#menu/activity_side_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Android Studio also generated an Activity class as part of the template. My app has a main activity as well that uses a LinearLayout. I want to use Fragments to add the drawer menu to the main activity (and other activities), but I'm unsure how to do this. Do I need to create a new subclass of fragment and adapt DrawerMenuActivity's callback methods for the fragment class? Then do I add a fragment tag to activity_main.xml?

This will likely be something you need to wrap your head around...
Ideally, you want to swap fragments in a container located in the the xml for the MainActivity by using fragment transaction and calling replace with the fragment to be placed in the container. This will allow you to re-use the container and offer the drawer to the entire app.
Otherwise, you would need to include the drawer in every activity which creates a lot of extra code or not offer the drawer which depending on the activity you may be able to get away with that.
So, the drawer works with fragments being swapped in via the transaction and not so well for additional activities. Its something you need to design around.
Please, create a new project with a generated drawer for an Activity and proceed from there. Also read the android developer tutorial for fragments part on communication to see how the drawer fragment and activity communicate.
There's actually quite a bit involved for the drawer but you should be able to get a good grasp in a day or two.

Related

Using a DrawerLayout without navigation functionality

I have been looking into DrawerLayout for my project's slideout menu. It seems to behave the way I want, except for one thing.
I've run a few sample projects and they all seem to have one thing in common: the DrawerLayout is always the main part of the application, as in:
setContentView(R.layout.activity_main);
Where activity_main is:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Also, DrawerLayout seems to have strong ties to navigation, where it's used to jump between fragments.
My problem is, I do not wish to use this slideout menu for navigation, nor do I have any desire to make my app's current main page a Fragment. I want a slideout menu, but I will be using it for settings mostly, not to move around the app.
Is there any way to have a simple slideout menu, which behaves visually and tactically like DrawerLayout, but without the need to have all my views set as fragments of it?
I have recently done this using 1. a custom RelativeLayout class with a ViewTreeObserver referencing a custom X or Y property (for drawer you would use X) 2. objectAnimator XML referencing same custom property 3. Fragment class for the UI itself, using the custom Layout class 4. use setCustomAnimations method of getFragmentManager to show / hide the menu.
Here's an example, just change it to horizontal
Fragment Translation Animation for a Menu

Android - How to reuse a wrapper layout for different layout XMLs?

I want to make every activity have a same wrapper layout like a drawer or a bottom toolbar. It's easy to create a BaseActivity to reuse the code for all activities. But how can I reuse the XML wrapper for all the different activity layouts instead of adding different contents into a same XML file dynamically?
For example, I have 5 XML layout files. They should have a same wrapper, and after I modify one of them, the others should be changed synchronously. A most simple example could be a drawer:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
// different content here
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
You can create an activity and add/remove fragments within it, this way the activity will hold all the common UI components and the distinct views will be in the fragments
http://developer.android.com/training/basics/fragments/index.html
You can create the layout separated and use tag, and, all your activities will inherit a "BaseActivity" which will set the content, and then, in your child activity, it will find the ID for the "content", and replace it.
Does that work for you?
Note that the content may be a simple ViewGroup to be replaced.

create navigationdrawer and call previous activity

I have a running app with many activity. Currently i have buttons and onclick event related activity get open. Now i have created a new navigationdrawer activity [provided by android eclipse] and listed all button options in the drawer list menu. Now i want to call my activities onclick of the list menu selection. I have tried and find out many solution but failed to implement.
Is any one let me know in which function i have write code to call my activity? Is it onSectionAttached() if its so then how? when i have tried with Intent.. and start avtivity the new activity get loaded and Navigation drawer get disable.
Do i have to create new fragments classes?
It sounds like you have two options. You can use an intent and then implement a drawer in the second activity. Not ideal because this leads to a lot of code duplication.
OR (better)
You can use another layout for your activity. Something like this:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
Then you can use fragments in the framelayout. Then just use fragmentmanager and replace the fragment in the frame for the one the user clicks on in the drawer.
Check out http://developer.android.com/training/implementing-navigation/nav-drawer.html for more info.

How to create sliding layout like in Google Drive tablet app or Google+ notifications?

I'd like to create an extra-information view similar to that of the Google Drive app (below) on a tablet. When the info button is clicked, this view slides in from the rightcontaining a layout. Another example would be the Google+ app with its notifications slide-out panel:. The SlidingLayer by 6Wunderkinder almost works, but doesn't fade a semi-black background over the views behind the "drawer" and I haven't found another library that does this.
If anybody has any suggestions/solutions please let me know!
Also, I've already looked at this question and none of the answers suggested there are correct either.
For posterity, here's the answer to this question. As Steve Benett's suggestion led me to discover, the correct way to do this is to use two DrawerLayouts, nested within each other like so:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_navigation_bar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_sidebar"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_main_content"
android:name="MainContentFragment"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<fragment
android:id="#+id/fragment_sidebar"
android:name="SidebarFragment"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
</android.support.v4.widget.DrawerLayout>
<fragment
android:id="#+id/fragment_navigation_bar"
android:name="NavigationFragment"
android:layout_height="match_parent"
android:layout_width="300dp"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
The innermost DrawerLayout contains the main content of the Activity, whether it be a fragment or some other layout components. fragment_sidebar is the fragment that will be swiped out from the right. Then, on the top-level DrawerLayout you have the fragment_nagivation_bar which houses the left Drawer's ListView or whatever.
Then, in the Activity Java code you have:
mDrawerLayoutLeft= (DrawerLayout) findViewById(R.id.drawer_navigation_bar);
mDrawerLayoutRight = (DrawerLayout) findViewById(R.id.drawer_sidebar);
mDrawerLayoutLeft.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerLayoutRight.setDrawerShadow(R.drawable.sidebar_shadow, GravityCompat.END);
An optional addition (but recommended, for consistency of UX) is to hide the other Drawer when one is opened, so your screen doesn't consist solely of Drawers.
I hope this has helped somebody!
This is the DrawerLayout. Have a look at the design guide, which illustrates the behavior well.
If you want to use / customize the "semi-black background" use DrawerLayout.setDrawerShadow() with a drawable. Google hands out a set of drawables here. Download the ActionBar Icon Pack and look for the drawable_shadow.9.png.
If you want that the menu appears from the right, set android:layout_gravity="end" as a property in the second child of the layout.

Android NavigationDrawer and ViewPager as its one of fragment

I have created a SherlockFragmentActivity that has three fragments for ViewPager. But now I want to use this Activity in NavigationDrawer but I am confused how to do this. Google+ app has this kind of implementation but I am wondering about how to achieve this.
NavigationDrawer will have following UI elements:
FragmentActivity(contains three fragment as ViewPager)
Second Fragment
Third Fragment
Is this kind of layout possible with Navigation Drawer If yes, how should I do it. If not, what should I do to achieve this kind of navigation in my app.
It is very much possible. You just have to create the right layout file for it.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<RelativeLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- viewpager goes here -->
</RelativeLayout>
<!-- The navigation drawer -->
<LinearLayout android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start">
<!-- fragment one goes here in drawer -->
<!--- fragment two goes here in drawer-->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
A DrawerLayout has to have 2 childs, the first is the main content, in your case this will contain the ViewPager and perhaps other stuff. The second child is the Drawer itself.
Use this as the content of your activity.
The rest of the information can be found by using the example on the android developers page here
You can use below libraries to get navigation model similar to your requirement
ActionBarSherlock (github)
nested-fragments (github)
PagerSlidingTabStrip (github)
NavigationDrawer (Android developer site)
Latest Support v4 library
Have a look at my post
Below is screenshot of my sample app Navigation Drawer with Tab Strip Example at github

Categories

Resources