Reputation: 5140
The title explains it all. All what I want to do is to know if the navigation drawer is open or not. I searched a lot on the net and found the method isDrawerOpen(int drawerGravity)
but couldn't find a satisfactory answer which explains how to use it in a method. I would appreciate if anyone explains it to me.
Upvotes: 98
Views: 74694
Reputation: 4563
To add to the other answers about using DrawerLayout#isDrawerOpen
:
I found that if I use that method in onStart
(or an earlier lifecycle callback!) to detect if a drawer is still open after screen rotation (or a similar configuration change), then it would return false
even if the drawer was open. Calling this method from onResume
does return true
and false
for a drawer that is respectively open and closed after configuration change.
Upvotes: 0
Reputation: 2324
May be, I am late but I can share new updated information.
In given code, I set open/close drawer in onclick function.If drawer isopen than it willbe closed else open.This solution is based on RXava DrawerLayout (androidx.drawerlayout.widget.DrawerLayout)
.
In my code I open drawerlayout from right to left.
@Override
public void onClick(View view) {
if (drawerLayout.isDrawerVisible(GravityCompat.END)) {
drawerLayout.closeDrawer(GravityCompat.END);
}else {
drawerLayout.openDrawer(GravityCompat.END);
}
}
Note: If you open drawerLayout from right side use "GravityCompat.END" and it is open from left side, than use "GravityCompat.START". Enjoy codding.....
Upvotes: 3
Reputation: 17576
The method is the same in Kotlin.
Initialize the DrawerLayout View
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
Check if the drawer is open
if(drawerLayout.isDrawerOpen(GravityCompat.START)){
Log.d("Drawer","open")
}
If you want to take actions automatically when the drawer is opened or closed, you can do the following.
In your main activity, create an inner class that is a subclass of DrawerLayout.DrawerListener. The DrawerLayout class implements the DrawerListener interface.
inner class CustomDrawer : DrawerLayout.DrawerListener{
override fun onDrawerStateChanged(newState: Int) {
}
override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
}
override fun onDrawerClosed(drawerView: View) {
imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
}
override fun onDrawerOpened(drawerView: View) {
imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
}
}
Put your action in the function you want to use. In my example, I'm closing the soft keyboard if the user interacts with the navigation drawer. Declare the InputMethodManager like this in your main activity:
var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)
Add your custom DrawerListener to the drawerLayout (I put it in the onCreate method)
var drawerListener = CustomDrawer()
drawerLayout.addDrawerListener(drawerListener)
Upvotes: 1
Reputation: 247
By the following way you can find the Drawer is open or close..
public class YourActivity extends AppCompatActivity implements DrawerLayout.DrawerListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dash_board);
DrawerLayout drawer=(DrawerLayout)findViewById(R.id.container);
drawer.setDrawerListener(this);
}//onCreate()
@Override
public void onDrawerOpened(View arg0) {
//write your code
}
@Override
public void onDrawerClosed(View arg0) {
//write your code
}
@Override
public void onDrawerSlide(View arg0, float arg1) {
//write your code
}
@Override
public void onDrawerStateChanged(int arg0) {
//write your code
}
}//class
Upvotes: 6
Reputation: 60923
Detect DrawerLayout
opened, closed, slide DrawerLayout.DrawerListener
DrawerLayout drawerLayout:
drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
/**
* Called when a drawer's position changes.
*
* @param slideOffset The new offset of this drawer within its range, from 0-1
* Example when you slide drawer from left to right, slideOffset will increase from 0 - 1 (0 when drawer closed and 1 when drawer display full)
*/
@Override
public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
}
@Override
public void onDrawerOpened(@NonNull View drawerView) {
// do something when drawer opened
}
@Override
public void onDrawerClosed(@NonNull View drawerView) {
// do something when drawer closed
}
/**
* Called when the drawer motion state changes. The new state will
* be one of {@link #STATE_IDLE}, {@link #STATE_DRAGGING} or {@link #STATE_SETTLING}.
*/
@Override
public void onDrawerStateChanged(int newState) {
}
});
Check Drawer is opened
if(drawerLayout.isDrawerOpen(GravityCompat.START)) // or GravityCompat.END
Upvotes: 4
Reputation: 1637
shakeJ's answer its the correct one, and remember you can use the onDrawerSlide to be able to fire other functions... for example I used it to change the StatusBar Color.
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
isOpen = false;
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
isOpen = true;
}
public void onDrawerSlide(View drawerView,float slideOffset){
super.onDrawerSlide(drawerView,slideOffset);
if(!isOpen){
setStatusBarColor("#00102b");
}
if(isOpen){
setStatusBarColor("#EFEFF0");
}
}
Upvotes: 2
Reputation: 126455
Use:
mDrawerLayout.isDrawerOpen() method
Example:
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(Gravity.LEFT); //CLOSE Nav Drawer!
}else{
mDrawerLayout.openDrawer(Gravity.LEFT); //OPEN Nav Drawer!
}
Upvotes: 20
Reputation: 643
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
That listener use:)
Upvotes: 36
Reputation: 16164
Assuming you have defined a drawerlayout in xml:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
}
Upvotes: 221