I Want to change the navigation drawer toggle icon for close and open.
I downloaded the sample code from this link:
http://vardhan-justlikethat.blogspot.in/2014/02/android-slider-from-right-to-left.html
But there is no method call when navigation drawer closed.
I required to change that toggle button with some other image,
Thanks.
in res/drawable-hdpi/ ,res/drawable-mdpi/ , res/drawable-xhdpi/ and res/drawable-xxhdpi/ you have ic_drawer.png change that with your image
DrawerListener:
DrawerListener drawerListener = new DrawerListener() {
#Override
public void onDrawerClosed(View drawerView) {
drawerHandleImage.setImageResource(R.id.closed);
}
#Override
public void onDrawerOpened(View drawerView) {
drawerHandleImage.setImageResource(R.id.opened);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) { }
#Override
public void onDrawerStateChanged(int newState) { }
};
Set the listener to your DrawerLayout:
drawerLayout.setDrawerListener(drawerListener);
Related
I want to change the items on my navigation drawer based on the user that has logged in and if they have premium account or not.
I know how to make items visible and invisible, but I was wondering if I can put a function somewhere that when ever it's toggled it would set the visibility of them items and also change the header based on the user profile.
thank you all in advance.
Add a DrawerListener to your DrawerLayout
mDrawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerSlide(#NonNull View drawerView, float slideOffset) {
}
#Override
public void onDrawerOpened(#NonNull View drawerView) {
}
#Override
public void onDrawerClosed(#NonNull View drawerView) {
}
#Override
public void onDrawerStateChanged(int newState) {
}
});
I suggest you add a DrawerListener to your DrawerLayout
drawerLayout.addDrawerListener(new DrawerToggle());
DrawerToggle is an extension of android.support.v4.widget.SimpleDrawerListener in which you've overidden onClick() and onDrawerSlide()
#Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
}
You can use these methods to catch drawer toggle event
I have a toolbar with the "back button" on the left side and a button to to open the navigation drawer fragment on the right side. Now, if I click on the button on the right side and then on the back button on the left side, the app crashed and said : "No drawer view found with gravity LEFT". Yeah the fragment is on the right side and the question is how can I avoid this? Here is my code:
in OnCreate:
setSupportActionBar(toolbarTutorial);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
in onOptionsItemSelected:
if (id == R.id.editInformations) {
drawerLayout.setDrawerListener(new ActionBarDrawerToggle(ProfileActivity.this,
drawerLayout, toolbarTutorial, R.string.drawer_open,R.string.drawer_close){
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
mSlideState = false;
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mSlideState = true;
}
});
clickEventSlide();
}
and the clickEventSlide method:
public void clickEventSlide(){
if(mSlideState){
drawerLayout.closeDrawer(Gravity.END);
}else{
drawerLayout.openDrawer(Gravity.END);
}}
Once, my drawer icon changed from hamburger to the back button calling this:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
So, the back arrow is shown, but clicking on it , the drawer is still opening.
I would like to be able to handle it, to call onBackpressed() instead.
This is when i make the instance of the drawer
mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) return;
getActivity().invalidateOptionsMenu();
}
#Override
public View.OnClickListener getToolbarNavigationClickListener() {
return super.getToolbarNavigationClickListener();
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) return;
if (!mUserLearnedDrawer) {
mUserLearnedDrawer = true;
saveSharedSetting(getActivity(), PREF_USER_LEARNED_DRAWER, "true");
}
getActivity().invalidateOptionsMenu();
}
};
I suppose that getToolbarNavigationClickListener() method must be called when I tap on the drawer toogle, no matter if its hamburger or back arrow, but it does not.
When you need to override Arrow on toolbar click, set navigation click to toolbar.
getSupportActionBar().setNavigationOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//handle back press or open drawer etc.
}
});
It works for me every time.
It is possible to just show the Android default hamburger navigation drawer without change to back arrow feature?
The animation you're referring to is handled in the onDrawerOpened(), onDrawerClosed(), and onDrawerSlide() methods of the ActionBarDrawerToggle class. To disable it, simply override those methods without calling through to the super's methods. For example:
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(...) {
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerOpened(View drawerView) {
}
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
}
};
<-- the drawer icon
If you create, with android studio, a project with a navigation drawer, when you open/close the drawer, there will be a smooth animation of the drawer icon.
If I add a drawer listener to my drawerlayout, there is no more animation the drawer icon doesnt change anymore:
DrawerLayout dl = (DrawerLayout) findViewById(R.id.drawer_layout);
dl.setDrawerListener(new ActionBarDrawerToggle(this, dl,R.drawable.ic_drawer,R.string.navigation_drawer_open,R.string.navigation_drawer_close));
I tried to override methods of ActionBarDrawerToggle to add calls to syncState().
DrawerLayout dl = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, dl,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerSlide(View drawerView, float slideOffset) {
super.onDrawerSlide(drawerView, slideOffset);
mDrawerToggle.syncState();
}
#Override
public void onDrawerStateChanged(int newState) {
super.onDrawerStateChanged(newState);
mDrawerToggle.syncState();
}
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
mDrawerToggle.syncState();
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mDrawerToggle.syncState();
}
};
dl.setDrawerListener(mDrawerToggle);
Now, when the drawer is open, I have a small icon, and then it switch to a large icon when the drawer is closed, but I dont have the smooth animation.
Does somebody know how to get the smooth animation?
Try to put the #Override annotations that are missing for the last two listeners and remove all syncState() calls. Call syncState() from your Activity's onPostCreate:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
I have had the same problem.It's been a long time since the question was asked, but if you are still interested, here is my answer.
put
invalidateOptionsMenu();
instead of
syncstate();
for onDrawerClosed(), onDrawerOpened(), onDrawerSlide(), onDrawerStateChanged()
Also, put
#Override
protected void onPostCreate(Bundle savedInstanceState){
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
Intilize the boolean as false..private boolean isDrawerOpen = false;
public void onDrawerSlide(View drawerView, float slideOffset) {
if(slideOffset > .55 && !isDrawerOpen){
super.onDrawerSlide(drawerView, 1);
onDrawerOpened(drawerView);
isDrawerOpen = true;
} else if(slideOffset < .45 && isDrawerOpen) {
super.onDrawerSlide(drawerView,slideOffset);
onDrawerClosed(drawerView);
isDrawerOpen = false;
}
}
public void onDrawerClosed(View view) {
getActionBar().setTitle("Your title");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("your title");
invalidateOptionsMenu();
}
};