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!
Related
Apparently v4 api looks like this
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
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);
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()
}
};
But it has been depreciated in latest SDK. The new v7 API has constructor like this
public ActionBarDrawerToggle (Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes)
There is no parameter which takes in custom drawable resource file (Hamburger menu). How can I customize it? I tried setting navigationIcon of the toolbar but it kinds of disable navigation drawer itself. (Nothing happens when you click it).
In my application, I'm using a Navigation Drawer. I have given each item in the Navigation Drawer a different Icon for opening the Nav Drawer.
When I initially start the app, the drawer icon for the first fragment animates like normal. But when I click on another Nav Drawer Item, the animations break.
In my MainActivity, I have this code for toggling the nav drawer:
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ab_mytasks, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
Then in each of my fragments, I have this code for setting the custom icon:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
final ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setHomeAsUpIndicator(R.drawable.ab_mytasks);
}
I have tried putting in the same mDrawerToggle method as in my MainActivity into my fragments. But the app crashes when I use getActivity().invalidateOptionsMenu().
Here is an image representation of my problem:
1 = Animation works as normal
2 = Selected another fragment from Nav Drawer
3 = Original Fragment icon animation is broken
When you change the icon (setHomeAsUpIndicator) you will no longer get an animation.
In my android app, I have navigation drawer and action bar drawer toggle icon with ic_menu icon which was working fine. Yesterday I added new dependency in my gradle file
compile 'com.android.support:design:22.2.0'
after added this, in main activity instead of ic_menu icon home up icon is appearing. If I click that it is working like drawer toggle. The only issue is icon.
Please help me to solve this issue.
find the below code which I used to initialize action bar drawer toggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, navDrawerLayout, R.drawable.ic_menu_white_24dp, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};
navDrawerLayout.setDrawerListener(actionBarDrawerToggle);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Thanks,
Sundar
I was trying to implement the android.support.v4.app.ActionBarDrawerToggle in my app; since this class is deprecated
This class is deprecated. Please use ActionBarDrawerToggle in
support-v7-appcompat.
I've switched to android.support.v7.app.ActionBarDrawerToggle.
Before I could call the constructor in this way:
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) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
but after I've switched to the newer v7 support library, I receive the error
"ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to:
toolbar: android.support.v7.widget.Toolbar
Actual arguments: R.drawable.ic_drawer (int)"
Apparently I am not introducing a proper Toolbar into the constructor, but I'm not sure to understand the difference between the two conflicting arguments. How do I get the required toolbar?
I solved my problem by importing the newer android.support.v7.app.ActionBarDrawerToggle and by using the RecyclerView instead of the ListView as shown in this example: How to make Material Design Navigation Drawer With Header View:
private ActionBarDrawerToggle mDrawerToggle;
//... ...
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.drawer_open, R.string.drawer_close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// code here will execute once the drawer is opened
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
// Code here will execute once drawer is closed
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
};
If you still have trouble check here:
How to replace deprecated android.support.v4.app.ActionBarDrawerToggle
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.