Transparent Actiobar in android
I am trying to give a transparent effect to my action bar. I used the following line to get it to work this way, but oddly enough, on my Nexus 5, I see the panel fit, but on my galaxy nexus, I can't see it at all.
I'm not sure why this is happening on the N5? How to solve this problem?
Here is the code:
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
actionBar.setStackedBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.profile);
Here is a screenshot for both Galaxy nexus running Jelly Bean (4.3) and Nexus 5 running kitkat (4.4.4)
Galaxy Nexus: Shows transparent action bar perfectly:
Nexus 5: shows transparent action bar: (horizontal line displayed)
Update 2:
I was able to delete a line on N5 using the following style
styles.xml in Values-v14 folder
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar"></style>
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"></style>
then added theme to specific activity in manifext:
<activity
android:name=".Profile"
android:label="@string/profile"
android:theme="@style/CustomActionBarTheme"
android:screenOrientation="portrait" >
but then all my text, the format of the dialog changed in this action. It became dark. It doesn't change, although I programmatically change the topic of the dialogue. I'm not sure what is wrong? Can anyone help me with this?
Update 1: . On the advice of @erakitn: I've tried the following with minor modifications:
<!-- Transparent ActionBar Style -->
<style name="CustomThemeTransparent" parent="AppBaseTheme">
<item name="android:background">@android:color/transparent</item>
<item name="background">@android:color/transparent</item>
</style>
<!-- Activity Theme with transparent ActionBar -->
<style name="CustomThemeTransparentLight" parent="AppBaseTheme">
<item name="android:actionBarStyle">@style/CustomThemeTransparent</item>
<item name="actionBarStyle">@style/CustomThemeTransparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
I am getting the following error:
1. error: Error: No resource found that matches the given name: attr 'background'.
2. error: Error: No resource found that matches the given name: attr 'actionBarStyle'.
3. error: Error: No resource found that matches the given name: attr 'windowActionBarOverlay'.
source to share
To remove the ActionBar shadow, try adding the attribute <item name="android:windowContentOverlay">@null</item>
to your activity theme. Below is an example of a theme with a transparent ActionBar. Define it in res/styles.xml
:
<!-- Your App Theme-->
<style name="YourAppTheme.Light" parent="@style/Theme.AppCompat.Light">
<...>
</style>
<!-- Transparent ActionBar Style -->
<style name="YourAppTheme.Light.ActionBar.Transparent" parent="@style/Widget.AppCompat.Light.ActionBar">
<item name="android:background">@android:color/transparent</item>
<item name="background">@android:color/transparent</item>
</style>
<!-- Activity Theme with transparent ActionBar -->
<style name="YourAppTheme.TransparentActionBar.Light" parent="@style/YourAppTheme.Light">
<item name="android:actionBarStyle">@style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="actionBarStyle">@style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
And set this theme to your activity:
<activity
android:name="your.package.name.YourActivity"
android:label="@string/app_name"
android:theme="@style/YourAppTheme.TransparentActionBar.Light" />
UPD:
If you are not using AppCompat or ABS you should use the HOLO theme as a parent for your theme. You seem to be using a light theme with a dark ActionBar. Try the following solution:
<!-- Your App Theme-->
<style name="YourAppTheme.Light" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<...>
</style>
<!-- Transparent ActionBar Style -->
<style name="YourAppTheme.Light.ActionBar.Transparent" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">@android:color/transparent</item>
</style>
<!-- Activity Theme with transparent ActionBar -->
<style name="YourAppTheme.TransparentActionBar.Light" parent="@style/YourAppTheme.Light">
<item name="android:actionBarStyle">@style/YourAppTheme.Light.ActionBar.Transparent</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
source to share
Please don't change anything
The source code is fine. All you have to do is set / apply the following attribute:
android:windowContentOverlay = true
Again, don't change the theme / style. Define a new one, for example:
<style name="ConOver" > <<== no parent
<item name="android:windowContentOverlay">@null</item>
</style>
Add this above your code:
// Add this line
// Lets you add new attribute values into the current theme
getTheme().applyStyle(R.style.ConOver, true);
// Rest stays the same
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new
ColorDrawable(android.graphics.Color.TRANSPARENT));
actionBar.setStackedBackgroundDrawable(new
ColorDrawable(android.graphics.Color.TRANSPARENT));
setContentView(R.layout.profile);
source to share
Since you are using DarkActionBar
as a theme, now the dialog will turn black around because the style is being used DarkActionBar
.
You can place your dialog box from the standard HOLO theme for dialogs
.
Example:
final AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Light_Dialog));
EDIT:
Change this:
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo"></style>
For
<style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light"></style>
source to share