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
Related
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.
Im my app i have an Activity called MainActivity it contains a dynamic fragment with id container which i switch depending on state usually my app can be in the state Syncing or Regular now i wan't to add a NavigationDrawerto and i want it to be visible in the ActionBar and on the left only if the MainActivityis not on the Syncingstate since that if the Activity Synching i should not be abble to use the drawer to navigate in my app.
Im using the following activity layout
<!-- A DrawerLayout is intended to be used as the top-level content view using
match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="pt.ecs.myenergy.MainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="---------.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
The question is for a clever/best way to achieve this. I tought of a few but im not sure which one to use:
Have two xml layouts for the activity, one with the drawer included other without it and use setContentView which would cause for the xml to be inflated not sure if this is a good aproach
Use the merge tag with two childs one the drawer layout (with the container) other with only the container (for the syncing)
I would suggest doing none of those options. They both seem over complicate for no good reason for me.
you should lock the drawer closed using: drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
and if you're also using the an ActionBarDrawerToggle you disable it's indicator with drawerToggle.setDrawerIndicatorEnabled(false)
Requirement:- Action Bar Tabs using ViewPager with Navigation Drawer .
I can create a Navigation Drawer example
Action Bar Tabs using ViewPager separately.
But when I try to use both at once I am having issue.
I can create Navigation Drawer using fragments and Action Bar Tabs using Fragment. But the initial Activity of the both examples is Fragment Activity.
How to implement the action bar tabs on a fragment which is part of the navigation drawer?
The problem of using the tabs of the actionbar is when the drawer appears, it will appears under the tabs, since the tabs are part of the actionBar.
I have tried using tabHost instead and it works much better.
You get the source code here:
https://github.com/jiahaoliuliu/DrawerWithTabsAndViewPager
Here is a little explanation about it:
The first level there is a Drawer, from the v4 support library
Inside the drawer, the first element is the tabHost, which I have set the width and the height of the content to 0.
Under the tabhost, there is the viewpager.
Once everything has been created, what I have done is create a listener for the tabhost and another one for the viewPager, so when the user click on any tab the view pager will set the current item, and viceversa.
Enjoy Coding!
Use the following layout for your main activity.
<?xml version="1.0" encoding="utf-8"?>
<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" >
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffe6e1d4"
android:focusable="true"
android:focusableInTouchMode="true" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:listSelector="#drawable/drawer_list_selector"
android:background="#color/drawer_bg" />
</android.support.v4.widget.DrawerLayout>
Write your FragmentPagerAdapter as show in APPTabsAdapter.
This is how I had built it in one of my projects.
You can try and ask for help, if needed.
OR
You can take help from this GitHub Repo.
Thanks.
As you have noticed, ActionBar tabs don't play very nicely with Navigation Drawer and this design mode has been deprecated in API 21 anyway.
I used the classes in SlidingTabs example from Android developers to achieve this effect without having to include a 3rd party library dependency, and am very happy with the result. There is a video tutorial as well.
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.
I would like to know what is the type of Android Activity implemented in the image where you can put for example configurations and hide it with a swipe gesture
It is Navigation Drawer a panel that transitions in from the left edge of the screen and displays the app’s main navigation options.
You can find how to Create a Navigation Drawer here
<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 -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
The drawer view (the ListView) must specify its horizontal gravity with the android:layout_gravity attribute. To support right-to-left (RTL) languages, specify the value with "start" instead of "left" (so the drawer appears on the right when the layout is RTL).
It looks like Navigation Drawer ,The Navigation Drawer is a panel that transitions in from the left edge of the screen and displays the app’s main navigation options.
for more information you can visit developer Link
Following are the GitHub links where you can find Navigation Drawer source code
Navigation Drawer Source Code Link 1
Navigation Drawer Source Code Link2
Navigation Drawer Source Code Link 3
Navigation Drawer Source Code Link 4
More Collection
That is referred to either as a sliding menu or a navigation drawer.
Google released their own implementation for this last week: http://developer.android.com/training/implementing-navigation/nav-drawer.html
The most popular implementation prior to that was probably this SlidingMenu component.