Can't change drawer icon for NavigationDrawer - android

I'm trying to implement the new NavigationDrawer provided since the last Android keynote.
I got everything up and running, the navigation drawer opens and closes when pressing on the icon on the top left corner.
But now I still have the arrow icon although I replaced it with the ic_drawer from Android. Why?
Here's my code where I specified the icon:
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer, //<-- This is the icon provided by Google itself
R.string.drawer_open,
R.string.drawer_close
)
But the application still runs with the standard icon of setDisplayHomeAsUpEnabled.
Any ideas?

I just got the Navigation Drawer working. I had forgotten to add following methods also provided by the developer.android.com examples:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

I had the same problem the answer is if you are setting
getActionBar().setDisplayShowHomeEnabled(false);
then the normal up icon is shown. So try without using it

Related

How to keep closed the Sliding Menu even when user slides to open it? [duplicate]

This question already has answers here:
How to disable or hide a Drawer Layout from Fragment Android
(7 answers)
Closed 7 months ago.
In my application, I have some screens where I don't want the drawer menu to open when the user slides. How can I do that? The app uses only one activity and the rest are fragments.
How to keep closed the Sliding Menu even when user slides to open it?
Use below method in your Activity and call it as your need.
public void enableViews(boolean enable) {
if (enable) {
//You may not want to open the drawer on swipe from the left in this case
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Remove hamburger
toggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Doesn't have to be onBackPressed
onBackPressed();
}
});
} else {
//You must regain the power of swipe for the drawer.
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// Show hamburger
toggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
toggle.setToolbarNavigationClickListener(null);
}
}
Note:- Call enableViews(true) for locking and enableViews(false) for unlocking
Try this:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

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);
}

Android toolbar setNavigationIcon not working

So, I have a BaseActivity in which I have a toolbar and I call setSupportActionBar(toolbar).
In some of my activities that extends BaseActivity, I would like to change the navigation icon (the default arrow) to another drawable. But when I call toolbar.setNavigationIcon(myDrawable) it doesn't work, it still shows the default left pointing arrow icon.
Any idea?
Thanks.
I think you can set like this
menuDrawerToggle = new ActionBarDrawerToggle(this, menuDrawer, toolbar, R.string.drawer_open, R.string.drawer_close){...}
menuDrawerToggle.syncState();
toolbar.setNavigationIcon(getResources().getDrawable(yourDrawable));
put setNavigationIcon after syncState()
In my case: I don`t use ActionBarDrawerToggle. For me helpful was:
to change order of methods calls.
From:
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
To:
Toolbar toolbar = (Toolbar)getActivity().findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_chevron_left_white_24dp);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
In my case, setNavigationIcon after syncState as #Hsieh not work!
My solution is set in onPostCreate method as below.
Override this method in your activity
#Override
protected void onPostCreate(#Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mToolbar.setNavigationIcon(R.drawable.ic_menu_button);
}

Landscape + navigation drawer animation + new activity = bug?

Here's what's happening.. from my navigation drawer I select an item that then starts up a new activity. Now the thing is I want to close the navigation drawer before opening the new activity, but I don't want the delay to be too long so what I do is start the new activity with a postdelayed handler of about 200ms after a call to closeDrawer(). SO, the drawer is still closing when the new activity gets initialized. The problem kicks in when I go back to the original activity: the layout is shifted about halfway up the screen and tiled in a weird way. Doesn't seem to get fixed until you interact with it (so I'm assuming it gets fixed with the onDraw() call).
Conditions that make this crop up:
Device is in LANDSCAPE mode (doesn't happen on portrait)
The navigation drawer is animating out of view when startActivity() is called
Any insight? Bonus question: is there an easy way of making the navigation drawer close without the animation?
EDIT: Turns out the same thing happens if I'm in landscape and use startActivityForResult(). It's not an issue with only startActivity() though..
In your onAuthenticatedResume() of DrawerActivity, try configuring the DrawerManager & Toggle for drawer properly. The code looks like the snippet below ( I just added some dummy lines like 'YourDrawerManager' means whatever the manager you have )
#Override
protected void onAuthenticatedResume() {
super.onAuthenticatedResume();
//Configure NavigationDrawer
navigationDrawerManager = new YourDrawerManager();
mDrawerLayout = (DrawerLayout)findViewById(R.id.your_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.logo, "OK",
"Close") {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(your_title_string));
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
}
I've figured out what the issue was.
The problem was that in my AndroidManifest.xml I declared the new activities as having screenOrientation="portrait" and only enabled orientation changes in the onCreate method for them if certain conditions were met.
I'm still not 100% clear on what was happening but it seems that the device would try to rotate to portrait on activity change, then go back to landscape before drawing the new activity (since it was enabled in onCreate) and then that somehow messed up the previous activity so that when I went back to it, it would get shifted weirdly.
Still an Android bug IMO, but at least I know what it stems from now.
Changed screenOrientation to "landscape" and it works fine now (this problem doesn't arise when the first activity is in portrait and the second is locked to landscape in the manifest... what???)

use of syncState() in ActionBarDrawerToggle

I'm using ActionBarDrawerToggle class to tie together the functionality of DrawerLayout and the framework ActionBar to implement the recommended design for navigation drawers.
What Android developer site says is:
Call syncState() from your Activity's onPostCreate to synchronize the indicator with the state of the linked DrawerLayout after onRestoreInstanceState has occurred.
But I'm not getting what syncState() method actually does?
Please explain it as simple as possible.
The DrawerLayout indicator is the little icon to the left of the ActionBar home icon (see picture)
ActionBarDrawerToggle.syncState is called properly offset this indicator based on whether or not the DrawerLayout is open or closed after the instance state of the DrawerLayout has been restored.
Call syncState() from your activity's onPostCreate to set the state of the indicator based on whether the drawerlayout is in open or closed state once the activity has been restored with onRestoreInstanceState.
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Display the navigation drawer icon on action bar when there state has changed
super.onPostCreate(savedInstanceState);
drawerListener.syncState();
}
It is called from your Activity's onPostCreate to synchronize the indicator icon with the state of the linked DrawerLayout after onRestoreInstanceState has occurred.
for example
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
this.navDrawerToggle.syncState();
}

Categories

Resources