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.
Related
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.
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.
The question says it all. I know how to implement a drawer with the drawerlayout, but i have no idea how to place a button next to the drawer (to show the drawer) that moves along with the drawer.
You have to use ActionBarDrawerToggle.
ActionBarDrawerToggle myDrawerToggle = new ActionBarDrawerToggle(this, myDrawerLayout, R.drawable.my_icon_for_drawer_toggle,
R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
invalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
myDrawerLayout.setDrawerListener(myDrawerToggle);
I think what you want is sliding menu.
Look at this Github project which also provides example code for it
https://github.com/jfeinstein10/SlidingMenu
Hope this helped!
I'm currently trying to modify the UI of an existing Android application by making some slight changes. One change I need to implement is to modify the navigation drawer / slider toggle icon between one of 3 icons (on, off, 'special'). You supply the icon on creation of the ActionBarDrawerToggle on instantiation and there doesn't seem to be a method to modify the value after the object is created. Does anyone know if it's possible to achieve this (hack or no hack) ?
Thanks in advance,
thisguy
Check out this guide here: http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
and here: http://developer.android.com/training/implementing-navigation/nav-drawer.html
I think you may change icon on ActionBarDrawerToggle's methods:
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
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);