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)
Related
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
I am very beginner in android. I am trying to design a screen, in which I had selected the navigation drawer template when I created the project. So the basic things required are in place, like toolbar on the top and a drawer menu icon on the left. Now i want to place an imageview below the toolbar and a list view below the imageview. I am using the following xml for the layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
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" />
<ImageView
android:id="#+id/sliderTempImageView"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="fitXY"
android:src="#drawable/slider_image1" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
But the image covers the toolbar and fit the entire screen, even I had given fixed height of 100dp. Once the image view had set properly, I need to add list view also below the imageview.
My answer in comment : Add the ImageView in app_bar_main layout.
Reason why your code didn't work :
As per DrawerLayout specification
DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.
To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity because the XML order implies z-ordering and the drawer must be on top of the content . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
Note the statement position your primary content view as the first child. In your case you want to place an ImageView below the Toolbar which means you want to modify the primary content. And the primary content is located inside app_bar_main layout.
But the image covers the toolbar and fit the entire screen, even I had given fixed height of 100dp
It is because Drawers commonly use match_parent for height with a fixed width.
Also remove the RelativeLayout from the main layout as it is useless. Make the DrawerLayout as the root layout.
References :
https://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html
https://developer.android.com/training/implementing-navigation/nav-drawer.html
Give an id to the app bar include:
android:id="#+id/mToolbar"
And add this in your ImageView:
android:layout_below="#id/mToolbar"
Since you're using RelativeLayout, you can use the layout below, above and other attributes to position your views.
Hint: Avoid hardcoding your values. Use match_parent or wrap_content or use your dimens.xml file to declare your values.
Actually RelativeLayout have feature to arrange its child below/above/left/right/center/top/bottom one layout to other using below property
BELOW
ABOVE
LEFT_OF
RIGHT_OF
CENTER_IN_PARENT
ALIGN_PARENT_TOP
ALIGN_PARENT_BOTTOM
I use Activity in my main Activity. and I would like to go from my main Activity to navigation drawer activity by clicking button on my main intent.
I created navigation drawer from navdrawer-wizard in android studio.
But i got an error when i click the button.
I use Api level 10. Android support Library v7,v4 is also inside my project.
I use Android studio.
i've debugged that issue it tigger the start activity But it gots and error in logcat like this.
java.lang.RuntimeException: Unable to start activity ComponentInfo{www.epicmyanmar.com.andropos/www.epicmyanmar.com.andropos.MyActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
Any help or any suggestion would be appricieate
`
<!-- 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="www.epicmyanmar.com.andropos.MyActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
`enter code here`<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"
class="www.epicmyanmar.com.andropos.NavigationDrawerFragment"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>`
I got solutions By my self
Ker p peg is right.I found error in styles.xml .
Now i modified the style. normal Style for normal Activity.
and Nav-drawer style for Navdrawer activity.
Thanks every one
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 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