In this blog, we will learn about how we can change drawers color and behavior.
We all have seen the beauty of drawer layout and its usefulness as per our need while developing our Android applications.
But what many of us don’t know is the full capability of drawer layout.
In this blog, we will increase our knowledge about the drawer layout.
Firstly, let’s just start with changing the scrim color to our need. For those who are wondering what exactly the scrim color is, let me explain:
Do you notice, when we open the drawer, the screen on the right side goes dark, this is called scrim.
For changing this you just need to add a single line in your java code.
1 |
drawerLayout.setScrimColor(Color.RED); |
In the above code, you can replace RED with your own color of choice and also, if you don’t find the color already defined in the COLOR class, then you can use the hex value of your color as a string like in the example below :
1 |
drawerLayout.setScrimColor(Color.parseColor("#000000")); // Replace last 6 digit with the hex code of your color. |
Now we will try to change the behavior of drawer layout by scrolling our whole background with the drawer.
For this, all you need to do is override onDrawerSlide function of your ActionBarDrawerToggle.
and use as per your case.
1 2 3 4 5 6 7 8 9 10 11 12 |
new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){ @Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); float slideX = drawerView.getWidth() * slideOffset; if (mBinding.drawerLayout.isDrawerVisible(GravityCompat.START)){ contentLl.setTranslationX(slideX); // Replace contentLl with the first child of your drawer layout. }else if (mBinding.drawerLayout.isDrawerVisible(GravityCompat.END)){ mBinding.contentLl.setTranslationX(-1*slideX); } } }; |
Thast’s all.
For what I needed, I just had to override the onDrawerSlide function.
You can also explore other functions of the parent class as per your use case. Some of which might be useful are :
- onDrawerOpened
- onDrawerClosed
- onDrawerStateChanged