Show ImageView partly behind transparent ActionBar
You can enable overlay mode of the ActionBar
. To do it you have to set (android:)windowActionBarOverlay
item in the theme to true
.
<style name="MyTheme" parent="Theme.Sherlock">
...
<item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">true</item>
</style>
You can also set it at run-time:
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
This will the ActionBar a semi-transparent floating bar.
Like any requestWindowFeature...
, this should be called before adding content.
After the setContentView
, you can then set a background from your Drawable
with this:
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg));
Change getActionBar
with getSupportActionBar
for ActionBarSherlock
actionbar_bg.xml
with the root element of shape:
<solid android:color="#64000000" />
Although I find Tomik's solution great, this will be useful for those one-off cases for a few activities rather than an across the board style.
If someone needs the transparent bar but only for certain activities while using a solid one in the rest of them, it might be worth creating two different styles and using the manifest to get control over it:
<style name="MyThemeOverlay" parent="Theme.Sherlock">
...
<item name="windowActionBarOverlay">true</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">true</item>
<!-- any stuff common here, colours, etc -->
<!-- define the style for native ActionBar for Android 4 and higher -->
<item name="android:actionBarStyle">@style/myActionbarTransparent</item>
<!-- define the style for ActionBarSherlock -->
<item name="actionBarStyle">@style/myActionbarTransparent</item>
</style>
<style name="MyThemeNoOverlay" parent="MyTheme">
<item name="windowActionBarOverlay">false</item> <!-- for ActionBarSherlock -->
<item name="android:windowActionBarOverlay">false</item>
<!-- any stuff specific for no overlay activity action bars -->
<!-- define the style for native ActionBar for Android 4 and higher -->
<item name="android:actionBarStyle">@style/myActionbar</item>
<!-- define the style for ActionBarSherlock -->
<item name="actionBarStyle">@style/myActionbar</item>
</style>
<style name="myActionbar" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@color/white</item>
</style>
<style name="myActionbarTransparent" parent="@android:style/Widget.Holo.ActionBar">
<item name="android:background">@color/transparent</item>
</style>
and then in your AndroidManifest.xml
you can either use one of them as default and the other one for some specific activities by doing something like:
<application
...
android:theme="@style/MyThemeOverlay">
...
<activity
android:name=".Activity1"
/>
<activity
android:name=".Activity2"
android:theme="@style/MyThemeNoOverlay"
/>
<activity
android:name=".Activity3"
/>
<activity
android:name=".Activity4"
android:theme="@style/MyThemeNoOverlay"
/>
...
in styles.xml:
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="colorPrimary">@color/semiTransparent5</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
<item name="windowActionBarOverlay">true</item>
</style>
in activity_main.xml:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />