Drawer Layout stays open even after changing application - android

So i have a drawer layout inside Homepage of my activity it goes like this.
public void settingDrawer() {
if (drawer == null) {
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.setScrimColor(Color.TRANSPARENT);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
int width = getResources().getDisplayMetrics().widthPixels;
DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) navigationView.getLayoutParams();
params.width = width;
navigationView.setLayoutParams(params);
}
}
it works just fine, but when i change activity and came back (using back button, or home button) the Drawer Layout stays open, i tried to close it when i use startActivity() method but it just doesn't right. i think there is a mistake that i made but i don't know where.

When you are going to Next Activity then first close Drawer like this
drawer.closeDrawer(GravityCompat.START);
so the above code will be close your drawer before navigating to next page and when you back to Activity then drawer is already closed.

Related

ActionBarDrawerToggle does not open drawer until first slide

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

Navigation Drawer Refreshed

I am following this answer on SO, which is titled as:
Change NavigationView items when user is logged
The code works fine but the content of NavigationView change when I restart the app. I want the content to be changed after I click Login or LogOut item menus
This is my code in onCreate() method:
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.addDrawerListener(toggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer1);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
and here is the islogin() method:
public boolean islogin(){
// Retrieve data from preference:
prefs = getSharedPreferences("UserLoginData", MODE_PRIVATE);
String username = prefs.getString("username", null);
if (username == null) {
return false;
}
else{
return true;
}
}
Any help would greatly appreciated! Thank You
Note: Though this question seems duplicate of some, but its just the title, contents are entirely different.
Though I didn't get answer, I am posting my solution here.
I did solve it a very simple logic and its working very great.
Step 1:
I first initialize Toolbar toolbar; globally above class.
Step 2:
Then I just create a simple method called: myDrawer() and wrap all my Drawer code inside it. Like this:
public void myDrawer(){
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.addDrawerListener(toggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
if(islogin())
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer2);
} else
{
navigationView.getMenu().clear();
navigationView.inflateMenu(R.menu.activity_main_drawer);
}
navigationView.setNavigationItemSelectedListener(this);
toggle.syncState();
}
Now I have hands over the Navigation and I can do anything with it, Like Refreshing and Calling again etc.
Step 3:
I am calling the method in Main Activity i-e: OnCreate
myDrawer();
Step 4:
And I am calling it every time I do logic of Signin and SignOut.
Wow! It's working like a charm.
PS: Just for the info purpose, Here is my onNavigationItemSelected, where I can handle Click Events:
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
Intent intent;
if (id == R.id.nav_item_item1) {
intent = new Intent(MainActivity.this, SomeClass1.class);
startActivity(intent);
} else if (id == R.id.nav_item_item2) {
intent = new Intent(getApplicationContext(), SomeClass2.class);
startActivity(intent);
} else if (id == R.id.nav_item_logout) {
// my other logic for signout
myDrawer();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
Credits:
User: manish jain Answer

Setting drawer to lock mode is also disabling the hamburger icon

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

android navigation drawer not showing title bar in kitkat

In my application , i created a navigation drawer using Android studio template.
It works perfectly in Android 5.0 above devices but the title bar does not appear in Kitkat devices (only statius bar can be seen) , I was unable to find a solution,
The below is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().show();
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) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Intent intent = getIntent();
linkNo = intent.getIntExtra(FirstMenuFragment.LINK_NO, 0);
//Make firstItem active
navigationView.getMenu().getItem(linkNo).setChecked(true);
}
Thanks in advance
In KitKat, making the statusbar (semi-)transparent is more complex compared to Lollipop+. So the titlebar is still there, it's just below the status bar ;)
Edit: If you absolutely want to achieve this on KitKat, you could use a SystemBarTintManager (https://github.com/jgilfelt/SystemBarTint/blob/master/library/src/com/readystatesoftware/systembartint/SystemBarTintManager.java).
But I don't recommend that, I've done it myself and it's not so easy to get it right.

Open NavigationView only clicking on top-left icon but not with swype gesture

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

Categories

Resources