It is self Q&A post I have transparent ActionBar which overlays layout. After migration to the latest support library I forced to rid off action bar by tool bar. The old ways to make it transparent and overlay that layout doesn't work
It is self Q&A post I have transparent ActionBar which overlays layout. After migration to the latest support library I forced to rid off action bar by tool bar. The old ways to make it transparent and overlay that layout doesn't work
All you need to is to define theme which hide action bar, define action bar style with transparent background and set this style to the toolbar widget. Please note that toolbar should be drawen as last view (over all view tree)
Layout:
Toolbar layout:
The Toolbar works like a View, so the answer it's very simple.
toolbar.getBackground().setAlpha(0);
Checking Google's example Source Code I found out how to make the toolbar completely transparent. It was simpler than I thought. We just have to create a simple Shape drawable like this.
The name of the drawable is toolbar_bg
And then in the fragment or activity.. Add the toolbar like this.
And here we will have a fully transparent toolbar.
Don't add the
if you do, this won't work.
Note: If you need the AppBarLayout, set the elevation to 0 so it doesn't draw its shadow.
Add the following line in style.xml
Now add the following line in style-v21,
and set the theme as android:theme="@style/AppTheme"
It will make the status bar transparent.
Just use the following xml code:
Code for the layout:
And add the following code in the colors.xml:
#00FFFFFF
This will give you the desired output.
I implemented translucent Toolbar by creating two Theme.AppCompat.Light.NoActionBar
themes and setting colorPrimary
attribute to transparent color.
1) Create two themes Create one theme with for opaque Toolbar:
Second theme for transparent/overlay Toolbar:
2) In your activity layout, put Toolbar behind content so it can be displayed in front of it:
3) Apply the transparent theme to your acivity in AndroidManifest.xml
Perhaps you need to take a look at this post transparent Actionbar with AppCompat-v7 21
Points that the post suggest are
This should solve the problem without too much hassle.
The simplest way to put a Toolbar transparent is to define a opacity in @colors section, define a TransparentTheme in @styles section and then put these defines in your toolbar.
@colors.xml
#33000000
@styles.xml
@activity_main.xml
That's the result:
Just add android:background="@android:color/transparent"
like below in your appbar layout
`
I know am late for the party. I've created a simple class to manage the Toolbar
transparency.
import android.annotation.SuppressLint; import android.graphics.drawable.ColorDrawable; import android.support.v7.widget.Toolbar; public class TransparentToolbarManager { private Toolbar mToolbar; private ColorDrawable colorDrawable; public static final int MAX_ALPHA = 255, MIN_ALPHA = 0; public TransparentToolbarManager(Toolbar mToolbar) { this.mToolbar = mToolbar; this.colorDrawable = new ColorDrawable(mToolbar.getContext().getResources().getColor(R.color.colorPrimary)); } public TransparentToolbarManager(Toolbar mToolbar, ColorDrawable colorDrawable) { this.mToolbar = mToolbar; this.colorDrawable = colorDrawable; } //Fading toolbar public void manageFadingToolbar(int scrollDistance) { if (mToolbar != null && colorDrawable != null) { //FadeinAndOut according to the horizontal scrollValue if (scrollDistance = MIN_ALPHA) { setToolbarAlpha(scrollDistance); } else if (scrollDistance > MAX_ALPHA) { setToolbarAlpha(MAX_ALPHA); } } } @SuppressLint("NewApi") public void setToolbarAlpha(int i) { colorDrawable.setAlpha(i); if (CommonHelper.isSupport(16)) { mToolbar.setBackground(colorDrawable); } else { mToolbar.setBackgroundDrawable(colorDrawable); } } }
and the CommonHelper.isSupport()
public static boolean isSupport(int apiLevel) { return Build.VERSION.SDK_INT >= apiLevel; }
try below code
style.xml
goto res package and open color.xml set color primary to #00000000 its done.