I want to implement Navigation drawer it's working well but when i set app:elevation="0dp" in AppBarLayout at that time the navigation drawer is not opening. i have tried folowing code.
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.openDrawer(GravityCompat.START);
LOGD("Clicked:::","Drawer");
}
});
and also using toggle
drawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.app_name, R.string.app_name);
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_menu);
mDrawerLayout.setDrawerListener(drawerToggle);
drawerToggle.syncState();
but it's not working, i stuck from many days, pls help to solve it!!!
Related
I am using Google Design Support Library and DrawerLayout.
Setup:
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView drawer = (NavigationView) findViewById(R.id.drawer);
if(mDrawerToggle == null) {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, mToolbar, R.string.open, R.string.feather_close);
drawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
When I start my app, the ripple effect of the hamburger icon is shown but the drawer is not opened. When I open the drawer at least one time by sliding from the left, the hamburger icon works for the entire runtime.
I don't have a special listener on the toggle button or the drawer itself and the onOptionsItemSelected method is not called.
Please help me to find out what happens.
Thank you.
I found it: I accidentally had android:visibility="gone" in my NavigationView.
What a freaky side effect.
I solved it using it :
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Details: After changing the hamburger icon into a custom icon it does not respond on clicking (drawer does not open)
Here is the code snippet for oncreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contentViewId());
toolbar = (Toolbar) findViewById(toolbarId());
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (toolbarTitle() != null || !toolbarTitle().contentEquals(""))
getSupportActionBar().setTitle(toolbarTitle());
}
drawerLayout = (DrawerLayout) findViewById(drawerLayoutId());
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.app_name, R.string.app_name);
drawerLayout.addDrawerListener(drawerToggle);
navigation = (NavigationView) findViewById(navigationViewId());
navigation.setNavigationItemSelectedListener(this);
navigation.getMenu().findItem(selectedMenuItem()).setChecked(true);
drawerToggle.setDrawerIndicatorEnabled(false);
drawerToggle.setHomeAsUpIndicator(R.drawable.ic_account_balance_black_24dp);
drawerToggle.syncState();
}
More details:
the hamburger icon do change and also it respond when opening the drawer through slide but when i click the custom icon it does not..
Remove this line:
drawerToggle.setDrawerIndicatorEnabled(false);
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 am implementing the navigation view in my app.
Actually, I can open it both clicking on "hamburger" icon (in my toolbar) and swypeing from left to right.
I want to open it only through the icon in my toolbar disabling the swype. Is it possible to do that?
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, sToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
EDIT
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); // HERE
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, sToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
If I use the code above, where I used DrawerLayout.LOCK_MODE_LOCKED_CLOSED, I disable both icon and swype.
This works for my case
Lock it:
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
And unlock it :
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
Hope to help!
To make only the click work on the hamburger icon and not the swipe, i did the following,
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
mDrawerLayout.openDrawer(GravityCompat.START);
}
});
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);
}