Disable swipe for open drawer but NOT for closing - android

I want to disable the opening of a drawer via swipe, but not the closing via the swipe or back button.
I'm using fragments in my drawer, so that when the drawer opens I replace my fragment and add it to the backstack. On pressing the backbutton, the drawer closes as aspected.
But when I use
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
like in this post:
disable the swipe gesture that opens the navigation drawer in android
it disables ALL swipes und the backbutton navigation. The only way to close the drawer now is to touch the screen outside of the drawer.
Is there an alterantive to the LockMode and preserve the swipe close and backbutton navigation?
Note: I'm using Android 5.0.1

I found a workaround to achieve what I want:
Initialy I set the drawerMode to CLOSED. After opening it e.g. via a button, I UNLOCK the drawer to enable the gesture for closing again. After the drawer has been closed the Lock for opening will be reactivated. For this I'm using the Interface of the ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
)
{
#Override
public void onDrawerClosed(View drawerView)
{
super.onDrawerClosed(drawerView);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
#Override
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
};
I tried to overide the drawerlayout or parts of it, but it would be a hell of work to get what I want.
I hope this solution is helpfull to others.

Related

Navigation Drawer icon on the top right is showing Back arrow icon insted of Hamburger icon

I have Create a new android app with the Navigation Drawer Activity.
When i execute the app, the icon on the right corner is showing the back Arrow but it has to be the 3 line's icon.
The icon R.drawable.ic_drawer is this
In NavigationDrawerFragment class.
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
)
When i execute the App i'm getting Arrow icon on the right.
After the selection also the Icon is same.
how do i change it now ?
Hope this helps, I solved the above problem this way. #shkschneider is right. To make your code work import android.support.v7.app.ActionBarDrawerToggle
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout,
R.string.drawer_open,
R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
Sorry I'm little late but this might help other people also.
You just need to add the below line and it will work fine. :)
mDrawerToggle.setDrawerIndicatorEnabled(false);

How do you replace the default navdrawer icon in android?

I want to change the default DrawerLayout icon on the upper left side to my own image but I don't know why it does not work on load of app. The icon only changes when I have opened or closed the side menu. I also want to disable the animation, it is disabled after I changed the icon but that only happens after it is opened and closed. I don't know why this does not work on load of the activity
actionBar = (Toolbar) findViewById(R.id.custom_screen_toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, actionBar, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
actionBar.setTitle("Nav Menu Close");
actionBar.setNavigationIcon(R.drawable.action_bar_menu);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
actionBar.setTitle("Nav Menu Open");
actionBar.setNavigationIcon(R.drawable.action_bar_back_icon);
}
};
// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setSubtitleTextColor(getResources().getColor(
R.color.light_gray));
actionBar.setBackgroundResource(R.drawable.divider_action_bar);
actionBar.setNavigationIcon(R.drawable.action_bar_menu);
setSupportActionBar(actionBar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I believe calling setDisplayHomeAsUpEnabled(true) will cause the navigation icon to be set to the back arrow icon, in addition to providing the default behavior of finishing the activity when it's selected.
You should be able to remove that line and setHomeButtonEnabled(true) to get the effect you want.

Navigation Drawer does not move smoothly

I have created navigation drawer using sherelockActionbar libs. It is working fine and loading all fregments on available list of menus. but issue is it stops in middel when menu fragments load for few milliseconds in a perticuler one menu for other it moves smoothly . how to make it smooth for all menu.
I would suggest you try doing these fragment transactions after the navigation drawer closes completely. There is a method onDrawerClosed, where you could try to implement fragment swapping if a flag is set.
navDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
supportInvalidateOptionsMenu();
if(makeFragmentTransaction){
// make transaction; swap fragment
}
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
supportInvalidateOptionsMenu();
}
};
Just set the flag on drawer item click and after the drawer closes, it will check whether a transaction is to be made. If you decide to give it a shot, let me know how it works out for you.
Edit: updated the code.

android - open navigation drawer by clicking the app icon?

I use a navigation drawer in my app (the type of navigation that you open by sliding from the left side of the screen). Now, usually you can also open it by tapping the app icon, and when I looked it up, I found a whole bunch of code to add to your activity. And just for a simple button. I suppose thats not exactly what I am looking for? And if it really needs to be so much code for a single button, what is the best way to make the code more clear? Thank you, and sorry for being such an android newb.
I ran into this issue also, assuming you already have an ActionBarDrawerToggle as #Kernald suggested, you need to add the following also to your Activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
This let's the toggle handle the icon button press on the ActionBar, causing the Drawer to slide out.
Everything you need is described in the Navigation Drawer Guide from Google. Basically, you need to enable the "up" action on the ActionBar:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
Then you need to bind it to a Toggle:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

Toggle DrawerLayout With Title Only (NO App Icon)?

I have an activity with a DrawerLayout. I can open the drawer two different ways...by swiping from the left area of the screen towards the right and by clicking the app title. I DO NOT have an app icon displayed, only a title. I've implemented this exactly as recommended by Google here: Creating a Navigation Drawer: Open and Close with the App Icon
Everything is functional as far as opening and closing the drawer itself. However, it does not display the standard DrawerLayout icon that is suppose to be used. Instead I get the regular up caret (looks like a less than sign).
As soon as I add the app icon back to the ActionBar it begins to work as expected. The Drawer layout icon displays and animates when the drawer is opened or closed. I've tried removing the app icon both in my styles XML file and programmatically.
Is there a way to get the DrawerLayout icon working WITHOUT the app icon???
UPDATE: I've found a work around, but it's a hack more so than a solution. I simply created a 1x1 pixel transparent PNG (blank.png) and set it as my app icon in my styles.xml file. Below is all relative code:
styles.xml
<style name="MyCustomTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyCustomActionBar</item>
<item name="android:icon">#drawable/blank</item>
</style>
<style name="MyCustomActionBar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:displayOptions">showHome|showTitle|homeAsUp</item>
</style>
MainActivity -> onCreate()
this.navDrawerToggle = new ActionBarDrawerToggle
(
this,
this.navDrawerLayout,
R.drawable.icon_nav_drawer,
R.string.nav_drawer_open,
R.string.nav_drawer_closed
)
{
public void onDrawerClosed(View view) {}
public void onDrawerOpened(View drawerView) {}
};
MainActivity -> onPostCreate()
super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();
MainActivity -> onResume()
this.navDrawer.setOnItemClickListener(new DrawerItemClickListener());
this.navDrawerLayout.setDrawerListener(this.navDrawerToggle);
MainActivity -> onPause()
this.navDrawer.setOnItemClickListener(null);
this.navDrawerLayout.setDrawerListener(null);
MainActivity -> onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
navDrawerToggle.onConfigurationChanged(newConfig);
MainActivity -> onOptionsItemSelected(MenuItem item)
if (this.navDrawerToggle.onOptionsItemSelected(item)) {return true;}
else
{
// A bunch of item click handling happens here...
return true;
}
I was curious about this so I tried it with the sample for the DrawerLayout and it worked fine. Also, it seems like you have to use your own drawer icon drawable, it's recommended anyways. You can download the default assets from this site Creating a Navigation Drawer and put them in their respective drawable resource folder.
Here's what worked for me:
ActionBar actionBar = getActionBar();
// Let's get rid of the app icon here
actionBar.setIcon(null);
actionBar.setTitle("App Name");
// Setting these 3 options allows us to show the title as well as a "Home" elements
// "Home" elements include the Up and/or Drawer icon. The DISPLAY_HOME_AS_UP will enable
// and show the Drawer icon, we'll be "replacing" the "up" button with the drawer icon below
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE
| ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_HOME_AS_UP);
// Get a reference of the DrawerLayout
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLayout.setDrawerListener(drawerToggle);
// Setting ActionBarDrawerToggle will allow you to set the drawables for the drawer
// (this will also give you the nice/smooth animation) as well as allow you to do some
// other things depending on the events: onDrawerClosed & onDrawerOpened.
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_closed /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
actionBar.setTitle("Closed...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
actionBar.setTitle("Open...");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set a listener to be notified of drawer events.
drawerLayout.setDrawerListener(drawerToggle);
UPDATE: It seems like the android:displayOptions on the ActionBar style are not being accounted for. Use actionBar.setDisplayOptions(int options) instead.
getActionBar().setIcon(android.R.color.transparent);
This worked for me..... ;)
After setting the drawer toggle you need to call the following method:
mDrawerToggle.syncState();
so your code would look like this:
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
actionBar.setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
actionBar.setTitle("Preview Mode");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.syncState();
mDrawerLayout.setDrawerListener(mDrawerToggle);

Categories

Resources