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);
Related
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.
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.
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 change the navigation drawer position to a bit padded from the left like the new playstore icon . I have tried adding padding . But it doesn't work. Can anyone suggest a hint of how it should be achieved.
Have tried this but doesn't work
ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(50, 0,0,0);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, // nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
)
Used the v7-support library to solve the issue . Thanks to #tyczj .
import android.support.v7.app.ActionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
// nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
) {
#Override
public void onDrawerClosed(View view) {
toolbar.setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
toolbar.setTitle(mDrawerTitle);
getSupportFragmentManager().popBackStackImmediate();
supportInvalidateOptionsMenu();
}
};
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);
}