Simple side drawer issue in android - android

I implemented my left slide drawer for menu options using Simple-side-drawer Library. Its working fine but the issue is, the drawer layout menu options are responding to onclick events even after my drawer is closed state ie (menu options responding to onclick events from my MainActivity).

You can check SimpleSideDrawer's isClosed() method to make sure if it is closed.
SimpleSideDrawer menuDrawerLeft = new SimpleSideDrawer( this );
menuDrawerLeft.setLeftBehindContentView(R.layout.menu_drawer_left);
final Button buttonInLeftDrawer = (Button)findViewById(R.id.buttonInLeftDrawer);
buttonInLeftDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (menuDrawerLeft.isClosed()) {return;}
//runs only when the drawer is open
}
});

Without looking at your code it's hard to give any suggestions.
One advice though. Are you using this library:
https://github.com/adamrocker/simple-side-drawer
If so, please consider using the built-in Navigation Drawer that Google has released instead. It follows the design guidelines (which Simple Side Drawer does not) and is well tested to work.
See these resources for your implementation:
http://developer.android.com/design/patterns/navigation-drawer.html
http://developer.android.com/training/implementing-navigation/nav-drawer.html

Related

Material Drawer opens automatically as activity starts

When I use Material Drawer, as soon as a new activity starts, drawer is being displayed automatically, but I want that it starts hidden, so I have to use function drawer.closeDrawer() in the method onResume of activity, as described below:
#Override
protected void onResume() {
super.onResume();
drawer.closeDrawer();
}
Is this the correct way to hidden the drawer when activity starts or restarts, or there is a property to be set for this purpose in the drawer?
Thank you,
Alexandre Bianchi
There are different cases why the drawer may opens after the application starts.
Either you define withShowDrawerOnFirstLaunch, this should be removed or set to false, if you don't want this behavior.
https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/DrawerBuilder.java#L1188
It could also be that you open the drawer via the Drawer's API. So make sure you don't call openDrawer programtically
https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L125
The Drawer comes also with a method to close the drawer. Just call closeDrawer
https://github.com/mikepenz/MaterialDrawer/blob/develop/library/src/main/java/com/mikepenz/materialdrawer/Drawer.java#L134
Put this code in oncreate and it will check the drawer is open or not...if its open it will close the drawer
DrawerLayout layout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (layout.isDrawerOpen(GravityCompat.START)) {
layout.closeDrawer(GravityCompat.START);
}

neokrees Navigation Drawer doesn't show up

As a newbie in Android programming I'm playing with some Material Design. So I wanted to implement a Navigation Drawer. After looking for libraries I decided to use #neokrees MaterialNavigationDrawer (https://github.com/neokree/MaterialNavigationDrawer). I added it into my build.gradle and followed the instructions from the wiki.
As a base I'm using a MainActivity which holds a toolbar and a fragment.
I created a custom style for the drawer and wrote a simple NavigationDrawer subclass.
import it.neokree.materialnavigationdrawer.MaterialNavigationDrawer;
public class NavigationDrawer extends MaterialNavigationDrawer {
#Override
public void init(Bundle savedInstanceState) {
this.addSection(newSection("Section", new MainActivity.PlaceholderFragment()));
}
}
Then I added this to AndroidManifest.xml
<activity android:name=".NavigationDrawer"android:theme="#style/MyNavigationDrawerTheme" />
So everything compiles fine, the app starts up but nothing happens. So as first try I added a section to the drawer, relating to an activity. As I saw that the first item should be a fragment, I replaced it accordingly. Then I looked into the example app, but I don't get it to compile. Anyway, there is also no drawer-relevant code in the MainActivity.
So it's most probably a really dumb failure, but I have no idea anymore. So I would be thankful for any help.

Android DrawerLayout.setDrawerLockMode() not working

I have a Navigation Drawer (appcompat v7) in my app which is working perfectly fine.
Now I want to disable it, until the user buys an in-app-purchase to unlock additional functionality. So in my Activity.onCreate(), after initializing the drawer and populating it, I am calling this function:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
This function is not doing anything. The drawer continues to open and close as normal after tapping the drawer carat in the actionbar. I tried calling this function in Activity.onResume() without any difference.
What is the correct way to use this function?
(I tried looking online for answers, but couldn't find anything which addresses my issue). Any help is appreciated, as I am stuck on this issue for quite sometime now.
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
is only disabling the opening drawer layout by swiping till you click navigation drawer icon
keep a boolean variable
write mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); in onStart() and also write below lines of code
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if(!disabled)
{
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
mDrawerLayout.closeDrawer(mDrawerLinearLayout);
} else {
mDrawerLayout.openDrawer(mDrawerLinearLayout);
}
}
}
return super.onOptionsItemSelected(item);
}
this will work for sure
When you call setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) it locks opening and closing drawer only by swipes.
The drawer continues to open and close as normal after tapping the drawer carat in the action bar because your drawer will still respond to calls to openDrawer(int), closeDrawer(int) although a drawer is locked.
You need to add some logic in your action bar menu button listener and not to call openDrawer(int) when you don't want it to open.
Btw, it is okay to call setDrawerLockMode(int) in onŠ”reate
There is a bug with DrawerLayout and used gravity. I have reported it here:
https://issuetracker.google.com/issues/136738274

How to detect if slide menu is opened or closed using SlideHolder in android

I am using the slideHolder class for using slide menu in my project. I want to get notified in my fragment, when slide is opened or closed. I have tried the following line of code also:
SlideHolder.setOnSlideListener(SlideHolder.OnSlideListener);
But its giving error. How to detect the open and close event of the slide menu?
Any help will be appreciated. Thanks.
You can use a code similar to this. You need to implement your SlideHolderListener.
sliderMenu.setOnSlideListener(new SlideHolder.OnSlideListener() {
#Override
public void onSlideCompleted(boolean opened) {
if(opened)
// do whatever need once menu opened
else
// do whatever need once menu closed
}
});

How to open navigation drawer with animation at startup

I implemented navigation drawer as described here in android developer portal.
And everything works fine. Now I read android guidlines here. In section "Introduce the user to the drawer at first use" there is described I should open the drawer when application launches first. Now my idea for implementing this is to open the drawer after opening the app (and maybe close it again).
Now I tried to call myDrawer.openDrawer(Gravity.LEFT) in onCreate and the drawer is open when the app launches, but there's no animation. So onCreate seems to be the wrong place. Where should I place the call to openDrawer to let the user see the animation?
I guess you can do this by delaying the animation. For example:
#Override
protected void onResume() {
super.onResume();
myDrawer.postDelayed(new Runnable() {
#Override
public void run() {
myDrawer.openDrawer(Gravity.LEFT)
}
}, 1000);
}
However the fact that the Android Guidelines suggests the drawer to be opened when the application launches for the first time, does not imply it should be animated.

Categories

Resources