getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) { ... }
Okay, I double checked R.drawable.ic_drawer a few times. It is an icon with 3 bars, but my android display a left arrow. Anyone know what's wrong and how to fix it? Thanks in advance.
try to remove getActionBar().setDisplayHomeAsUpEnabled(true);
Call syncState() from your Activity's onPostCreate to synchronize the indicator with the state of the linked DrawerLayout.
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
Additionally onConfigurationChanged should be called on the ActionBarDrawerToggle, include this in your Activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
In your NavigationDrawerfragment class go to setUp method and do something like this with actionbar to set actionBar.setHomeAsUpIndicator() to ic_drawer just like below. It will remove back button and replace with ic_drawer button
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
}
Related
I want to disable the left swipe gesture for opening the navigation drawer as its messing with my seekbar. But setting drawer to LOCK_MODE_LOCKED_CLOSED is also disabling my hamburger icon.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
Can someone please tell me what am I doing wrong?
You're not doing anything wrong. They recently changed the behavior of ActionBarDrawerToggle to disable opening/closing the drawer if it's locked.
Since your Toolbar is the support ActionBar, a workaround is to remove the Toolbar argument from the ActionBarDrawerToggle constructor call. This will cause the Activity's onOptionsItemSelected() method to be called upon clicking the toggle, and there you can check the MenuItem's item ID, and unlock the drawer before calling the toggle's method.
The ActionBarDrawerToggle class works a little differently with an ActionBar than a Toolbar, so you'll need to add the following call to show the toggle.
getSupportActionBar.setDisplayHomeAsUpEnabled(true);
Then change your ActionBarDrawerToggle constructor call as follows:
drawerToggle = new ActionBarDrawerToggle(this,
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
};
And override the Activity's onOptionsItemSelected() method like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home) {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
drawerToggle.onOptionsItemSelected(item);
return true;
}
...
return super.onOptionsItemSelected(item);
}
I have refined the Navigation Drawer Activity project template of Android Studio, which uses Toolbar, v7.app.ActionBarDrawerToggle and NavigationView instead of the NavigationDrawerFragment (and layout/fragment_navigation_drawer.xml).
According to Google's guidance and reference, I set up ActionBarDrawerToggle. I made it 1) instantiate in onCreate, 2) call syncState in onPostCreate and 3) call through to onConfigurationChanged and onOptionsItemSelected.
It is almost perfectly working except for one thing: the hamburger icon is showed as an arrow.
A similar issue can be found on the StackOverFlow, especially for this question. But the question is about the way using the old R.drawable.ic_drawer as hamburger which is not the material design (before 5.0 Lollipop) version. Moreover the question has no answer and the questioner commented he had solved without stating any solution.
After a while, I have found a solution accidentally. It is somewhat dirty. It is to call syncState in onCreate. Because it seems that, for some reason, onPostCreate is not called in my app. Actually, this dirty solution is used in an answer to the other problem.
But the official reference says to call syncState in onPostCreate. Why doesn't it work? Why doesn't my app call onPostCreate? This should be main cause of not being showed hamburger icon (instead being showed arrow).
Below is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
drawerLayout.setDrawerListener(drawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
drawerToggle.syncState(); // calling this here is somewhat a dirty solution
}
#Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
}
Here onPostCreate:
#Override
public void onPostCreate(Bundle savedInstanceState,
PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
drawerToggle.syncState();
}
It should be this:
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
There are two types of onPostCreate:
Activity's onPostCreate with two arguments.
AppCompatActivity's onPostCreate with a single argument.
You should have made a mistake to choose the former one when you override a method on Android Studio.
I have problem with drawer indicator in toolbar.
The icon (hamburger) is visible only while moving (swiping) drawer view, and becomes invisible on stop.
Here is screenshot
http://imgur.com/EBGDq4z
And while moving it becomes visible
http://imgur.com/tEsAMLx
If stop move drawer view it becomes invisible again.
Here is my setup code
mToolbar.setVisibility(View.VISIBLE);
mToolbar.setTitle(getToolbarTitle());
setSupportActionBar(mToolbar);
if (hasDrawerToggle()) {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerMainLayout,
R.string.drawer_open, R.string.drawer_close);
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerMainLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
if (hasDrawerToggle()) {
mDrawerToggle.syncState();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (hasDrawerToggle()) {
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Why are you using hasDrawerToggle() condition?
The problem may due to your condition, so try by removing condition from all places like onPostCreate,onCreate etc.
Hop it will work.
I have a problem, I have different activity with different background color (white or blue), also the menu icon need to be white or blue (opposite to activity background), I can't find the correct way for change it after change activity, how can I do this?
U must do like this:
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
)
This in setUp() method.
And dont forget to add these code:
#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 am trying to implement a navigation drawer,Everything working perfectly except the toggle button is not visible.
Here is my Code:
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.list, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility)
also added these functions
#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);
}
How can i make it visible?
we have to enable following method
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);