ActionBarCompat & Transparency - android

I would like to make the ActionBar in the support library fully transparent, however, it seems that changing the background drawable won't suffice since the backgrounds stack. If you put a semi-transparent background you end up with the default background behind it.
Does anyone know a way to remove that background?
This is what happens:
The code for the background drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#66336688"/>
</shape>
As you can see, the drawable has a transparent blue that overlaps with the default gray background.

Ok, I found the solution messing around with the SDK.
It seems that it is pretty simple, you need to do 3 things:
Create a background drawable as shown on my question.
Create an ActionBar style like so:
<!-- Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ACTION BAR STYLES -->
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#drawable/actionbar_background</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="background">#drawable/actionbar_background</item>
<item name="windowActionBarOverlay">true</item>
</style>
Use the Window feature for ActionBar overlay using the Support method (ignore Eclipse's warning regarding API level for the constant; I used the SuppressLint annotation to remove the warning):
#SuppressLint("InlinedApi") #Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.activity_home);}

ChristianGuerrero answer is great but you should directly put the item:
<item name="android:windowActionBarOverlay">true</item>
inside the AppTheme style. Then you don't have to add anything in your onCreate method.

Related

ActionBarStyle from style.xml with AppCompat v7 support library

I searched whole web. Tried tens of solutions. I also followed documentation of Android but I couldn't manage to do want I need. I just want to make actionbar background gradient. I made my drawable xml file:
action_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="270"
android:startColor="#color/colorActionBarStart"
android:endColor="#color/colorActionBarEnd" />
</shape>
My style.xml:
<resources>
<!-- the theme applied to the application or activity -->
<style name="AppTheme"
parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyActionBarStyle</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBarStyle"
parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#drawable/action_bar</item>
</style>
</resources>
I saw same thing many times with android. There are tons of answers but none of them works. What I'm trying to do is just make a gradient background for actionbar. Isn't there anybody done this before with appcompat v7 library? Why is this so hard?
May be I can't get because I'm so new at android development. If this is the case tell me right point of view.
Please help me.
Thank you.
Personally, I do not use the default action bar provided by a theme like Theme.AppCompat.Light. Instead, I'll do this:
Use a theme like Theme.AppCompat.Light.NoActionBar
Include a <Toolbar> tag in my layout
Call setSupportActionBar() and pass my toolbar
This lets me set the background of my toolbar directly in my layout file. That said...
I tried playing around with the base themes you posted, and I was also unable to set the background of the default actionbar using styles. However, I got a gradient working by deleting the actionBarStyle theme item and just doing it in Java, in onCreate():
Drawable d = ContextCompat.getDrawable(this, R.drawable.action_bar);
getSupportActionBar().setBackgroundDrawable(d);
If your goal is to do this for every screen in your app, you could create a BaseActivity class and have all of your other activites extend that instead of AppCompatActivity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Drawable d = ContextCompat.getDrawable(this, R.drawable.action_bar);
getSupportActionBar().setBackgroundDrawable(d);
}
}

Remove action bar shadow

I want to remove the shadow that appears below the appcompat action bar so that the background of the action bar is completely transparent.
This is my theme and action bar style:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="android:actionBarStyle">#style/TransparentActionBar</item>
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/TransparentActionBar</item>
<item name="windowActionBarOverlay">true</item>
</style>
<!-- Transparent Action Bar Style -->
<style name="TransparentActionBar"
parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="android:background">#android:color/transparent</item>
<!-- Support library compatibility -->
<item name="background">#android:color/transparent</item>
<item name="elevation">0dp</item>
</style>
My minimum API level is 16
I've tried several solutions for this including:
Setting elevation to 0dp only works for Lollipop devices.
I get a "resource not found" error if I try to use windowContentOverlay
Setting the background of the root view to a color like white or transparent doesn't work
I've been trying to get this to work on 4.4.4 to no avail. Is it not possible below API level 21?
EDIT:
it turns out that the windowContentOverlay only works with the android prefix:
<item name="android:windowContentOverlay">#null<item/>
Trying to also define it without the prefix results in the resource not found error (this error points to the one with the prefix for whatever reason). I honestly don't understand why this occurs. I can only assume appcompat doesn't support the windowContentOverlay attribute.
Set windowContentOverlay to a drawable that will be drawn under the action bar. If you don't want a shadow set it to null, like so:
<item name="android:windowContentOverlay">#null</item>
and
<item name="windowContentOverlay">#null</item>
This works API level 16 and up.

Show the ImageView behind the Appcompat ActionBar

I am using theme as:
<style name="CustomActionBarTheme" parent="#style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
I want to show the ImageView partly behind the the ActionBar. I have tried Show ImageView partly behind transparent ActionBar and searched on google, but can't find the appropriate solution.
Better use Toolbar or Support Toolbar
With Toolbar, you can add it in your XML File just like any other View.
In your Activitys onCreate call setActionBar(toolbar) / setSupportActionBar(toolbar)
Or try to add these two lines in your style:
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
But using the Toolbar is actually pretty simple and the new standard.
Read this to get started: http://android-developers.blogspot.de/2014/10/appcompat-v21-material-design-for-pre.html

ActionBar padding between elements:

I am trying to put padding between items in the action bar - specially my logo and the Navigation drawer icon, following is the style I am using:
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar" parent="#android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:icon">#drawable/logo_header</item>
<item name="android:actionButtonStyle">#style/MyActionButtonStyle</item>
</style>
<style name="MyActionButtonStyle" parent="#android:style/Widget.ActionButton">
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
</style>
</resources>
However these two dont seem to work:
<item name="android:minWidth">32dip</item>
<item name="android:padding">12dp</item>
Any idea what I am missing here?
I'd make the logo into a drawable layer list instead and that way i can set its padding something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#drawable/my_main_icon"
android:right="20dp"/>
</layer-list>
And if you use this please update your manifest to use this drawable as the main icon instead of the current.
update: it might be better to use this icon as a logo as per reading on stackOverFlow.
so in your manifest in application tag set android:logo="my_main_icon" , researching this still.
update: if you use logo i think you have to turn it on in the actionbar...
setDisplayUseLogoEnabled() Enables the use of an alternative image (a
"logo") in the Action Bar, instead of the default application icon. A
logo is often a wider, more detailed image that represents the
application
i think the correct syntax here would be:
<item
...
android:layout_padding="12dp"
...
/>

Setting background color of android actionbar

I am operating with the compatibility library v7.
I am just trying to set the color of my actionbar (for Android 2.1 and above - though I run Android 4.4.2) to a solid color.
However the color does not change. It remains the same.
I have also tried creating a solid drawable with the color but that also does not change.
Finally I tested if I could change the backgroudn of my layout and I could - it must be something about the actionbar background which I'm not getting.
Here is the code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="CustomActionBarTheme"
parent="#style/Theme.AppCompat.Light.DarkActionBar">
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#0000ff</item>
</style>
</resources>
This seems to work for me. Try to use a resource instead of a raw value.
<style name="app_theme" parent="#android:style/Theme.Holo">
<item name="android:actionBarStyle">#style/app_action_bar</item>
</style>
<style name="app_action_bar" parent="#android:style/Widget.Holo.ActionBar">
<item name="android:background">#color/google_lightest_gray</item>
<item name="android:icon">#android:color/transparent</item>
<item name="android:windowActionBarOverlay">false</item>
</style>
use getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#808080"))); in Activity extends SherlockActivity or the color as your wish :)
You can use:
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.colorname));
use the following code getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bg_color.xml));
actionbar_bg_color.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/actionbar_bg"/> <!-- set desired #0000ff color in color.xml
you can use here solid color as well as gradient -->
</shape>
hope this help you
I solved it by:
Properly installing the support-v7-appcompat library.My mistake.
Shifting the styling to the res/values/styles.xml files for each version, i.e. one for backwards compatibility and one for values-v11.
I don't know why I couldnt get the result I wanted within themes.xml.
If anyone has an answer I'd be grateful

Categories

Resources