I am working on Android project and I am implementing the Navigation Drawer. I am reading through the new Material Design Spec and the Material Design Checklist.
The spec says that the slide out pane should float above everything else including the status bar and be semi-transparent over the status bar.
My navigation panel is over the status bar but its not got any transparency. I've followed the code from this SO post as suggested in the Google developers blog spot, link above How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?.
Below is my XML layout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/appPrimaryColour" />
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout"
android:layout_width="304dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:fitsSystemWindows="true"
android:background="#ffffff">
<ListView android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:choiceMode="singleChoice"></ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Below is my apps theme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/appPrimaryColour</item>
<item name="colorPrimaryDark">@color/appPrimaryColourDark</item>
<item name="colorAccent">@color/appPrimaryColour</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
</style>
Below is my apps v21 theme
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/appPrimaryColour</item>
<item name="colorPrimaryDark">@color/appPrimaryColourDark</item>
<item name="colorAccent">@color/appPrimaryColour</item>
<item name="windowActionBar">false</item>
<item name="windowActionModeOverlay">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
Below is my onCreate method
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout);
mDrawerList = (ListView)findViewById(R.id.left_drawer);
mDrawerLayout.setStatusBarBackgroundColor(
getResources().getColor(R.color.appPrimaryColourDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
LinearLayout linearLayout =
(LinearLayout)findViewById(R.id.linearLayout);
linearLayout.setElevation(30);
}
Below is a screenshot of my navigation drawer showing the top isn't semi transparent
If you want navigation panel is over the status bar and be semi-transparent over the status bar. Google I/O Android App Source provides a good solution (APK in Play Store are not updated to lasted version)
First you need a ScrimInsetFrameLayout
Then, in your XML layout change this part
Change LinearLayout to your ScrimInsetFrameLayout like this
You need to wrap your navigation drawer layout inside a
ScrimLayout
.A
ScrimLayout
basically draws a semi-transparent rectangle over your navigation drawer layout. To retrieve the size of the inset simply overridefitSystemWindows
in yourScrimLayout
.Later override
onDraw
to draw a semi-transparent rectangle.An example implementation can be found in the Google IO App source. Here you can see how it's used in the layout xml.
All you need to do is to use some semi-transparent color for status bar. Add these lines to your v21 theme:
Don't forget that the
color
(resource) must always be in the format of#AARRGGBB
. This means that the color will also include the alpha value.All answers mentioned here are too old and lengthy. The best and short solution that work with latest Navigationview is
this is going to change your status bar color to transparent when you open the drawer
Now when you close the drawer you need to change status bar color again to dark.So you can do it in this way.
and then in main layout add a single line i.e
and your drawer layout will look like
and your navigation view will look like
I have tested it and its fully working.Hope it helps someone.This may not be the best approach but it works smoothly and is simple to implement. Mark it up if it helps.Happy coding :)
Why not use something similar to this?
The code above uses the alpha resource in the
android:background
to be able to show transparency within the actionbar.There are other ways to do this through code, as other answers show. My answer above, does the necessary in the xml layout file, which in my opinion is easily edited.
I had a similar feature to implement in my project. And to make my drawer transparent i just use the transparency for hex colours. Using 6 hex digits, you have 2 hex digits for each value of red, green and blue respectively. but if you put two additional digits (8 hex digits), so you have ARGB ( two digits for alpha value)(Look here).
Here are the Hex Opacity Values: for the 2 additional digits
For exemple: if you have de hex colour (#111111) just put one of opacity values to get transparency. like this: (#11111100)
My drawer does not cover the action bar (like yours) but the transparency can be applied in these cases also. Here's my code:
And here is another article that can help you understand the alpha codes in hex colors.