17

How can i remove the drop shadow of action bar from java code ?.

If i remove from the style it is working fine.

<style name="MyTheme" parent="Theme.Sherlock">
....
<item name="windowContentOverlay">@null</item>
<item name="android:windowContentOverlay">@null</item>
....
</style>

But i need to remove and add it dynamically from java code.

2
  • Can i ask why you want to remove it using java?
    – LotusUNSW
    Nov 12, 2013 at 6:18
  • 1
    i want the shadow, below the actionbar for some activity and not for others.
    – John
    Nov 12, 2013 at 6:51

7 Answers 7

12
+25

There is no way to set value for windowContentOverlay attribute programmatically. But you can define two different themes, one for Activities with a visible ActionBar shadow and one for others:

<!-- Your main theme with ActionBar shadow. -->
<style name="MyTheme" parent="Theme.Sherlock">
    ....
</style>

<!-- Theme without ActionBar shadow (inherits main theme) -->
<style name="MyNoActionBarShadowTheme" parent="MyTheme">
    <item name="windowContentOverlay">@null</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Now you can set them to activities in AndroidManifest.xml:

<!-- Activity with ActionBar shadow -->
<activity
    android:name=".ShadowActivity"
    android:theme="@style/MyTheme"/>

<!-- Activity without ActionBar shadow -->
<activity
    android:name=".NoShadowActivity"
    android:theme="@style/MyNoActionBarShadowTheme"/>

Or you can set the right theme programmatically in onCreate() method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyNoActionBarShadowTheme);
    super.onCreate(savedInstanceState);

    //...
}
8

If you won't make more theme, then try this. This works nicely for me!

public void disableActionbarShadow() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        View v = getActivity().findViewById(android.R.id.content);
        if (v instanceof FrameLayout) {
            ((FrameLayout) v).setForeground(null);
        }
    }else {
        // kitkat.
        // v is ActionBarOverlayLayout. unfortunately this is internal class.
        // if u want to check v is desired class, try this
        //   if(v.getClass().getSimpleName.equals("ActionBarOverlayLayout")) 
        // (we cant use instanceof caz ActionBarOverlayLayout is internal package)
        View v = ((ViewGroup)getActivity().getWindow().getDecorView()).getChildAt(0);
        v.setWillNotDraw(true);
    }
}

From Kitkat(maybe), ActionBarOverlayLayout is included in activity's view tree.
This shows actionbar shadow (I think XD)

refs: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/com/android/internal/widget/ActionBarOverlayLayout.java#79

Becaraful I don't know whats happen if using support library version.

0
6

I know this question is old, but this problem was new for me, so it might help somebody else as well.

Try:

  • getSupportActionBar().setElevation(0);

Or, if you are not using a custom action bar:

  • getActionBar().setElevation(0);

I think this is the easiest way to do it programmatically.

4

Define a new style like this. Notice that there's no parent defined:

<style name="ConOver" >    <<== no parent
    <item name="android:windowContentOverlay">@null</item>
</style>

Add this above your code:

// Add this line before setContentView() call
// Lets you add new attribute values into the current theme
getTheme().applyStyle(R.style.ConOver, true);

// Rest stays the same
6
  • 1
    I don't know why this answer was downvoted, it is the way to remove the actionbar shadow programmatically and it works. Mar 3, 2015 at 12:23
  • probably because it only works BEFORE setContentView, which kind of defeats the purpose of doing it programmatically. Better to have different styles on the activity to begin with.
    – Greg Ennis
    Mar 29, 2015 at 23:45
  • @GregEnnis Hi Greg, I am not sure why you consider having two activities that only differ in the styles they use 'better'. Moreover, if you notice, we are adding/updating an attribute to/in the style - not using a different one. Can you point to one answer on this page that does not recommend - using two separate activities || recreating an activity || setting the theme before setContentView(...) .
    – Vikram
    Mar 30, 2015 at 2:46
  • @GregEnnis By the way, the (second part of) accepted answer has the same restrictions as mine does, was posted much after mine was, and recommends using two separate themes rather than overriding attributes at runtime. At the time of posting, I had pissed someone off on SO, and they were serially downvoting my answers. Within moments of posting, I had acquired a -2. No way anyone wants to even try an answer with that score :)).
    – Vikram
    Mar 30, 2015 at 2:49
  • @Vikram well I did not downvote you, actually I just voted you up because getTheme().applyStyle() is a nice tool to have in the bag of tricks. It certainly can be useful in other situations. Point taken about just changing one style should not require a completely different theme definition.
    – Greg Ennis
    Mar 30, 2015 at 2:53
3

Declare 2 styles ne has a shadow and the other one doesn't. THen in your manifest define each activity's theme with respective one. e.g

<Activity 
android:theme="@style/ThemeShadow" ...>

...

<Activity 
android:theme="@style/ThemeNoShadow" ...>
2

I found this article: Change Theme of Layout Runtime by Button Click in Android

It allows you to change the theme of an activity, but it does call finish() and restart the activity. So, I am not sure it will work for what you want.

import android.app.Activity;
import android.content.Intent;

public class Utils
{
     private static int sTheme;

     public final static int THEME_DEFAULT = 0;
     public final static int THEME_WHITE = 1;
     public final static int THEME_BLUE = 2;

      /**
       * Set the theme of the Activity, and restart it by creating a new Activity of the same type.
       */
      public static void changeToTheme(Activity activity, int theme)
      {
           sTheme = theme;
           activity.finish();

 activity.startActivity(new Intent(activity, activity.getClass()));

      }

      /** Set the theme of the activity, according to the configuration. */
      public static void onActivityCreateSetTheme(Activity activity)
      {
           switch (sTheme)
                {
                default:
           case THEME_DEFAULT:
               activity.setTheme(R.style.FirstTheme);
               break;
           case THEME_WHITE:
               activity.setTheme(R.style.SecondTheme);
               break;
           case THEME_BLUE:
               activity.setTheme(R.style.Thirdheme);
               break;
           }
      }
 }
1

If supportActionBar.elevation=0f did not work for you, You can remove the shadow by setting appBar.targetElevation=0f but this method is deprecated. The new method is using StateListAnimator.

You have to set the stateListAnimator to AppBarLayout without elevation that can be achieved by the steps below

  1. Create animator resource appbar_animator.xml

    <item
        android:state_enabled="true"
        app:state_collapsed="false"
        app:state_collapsible="true">
        <objectAnimator
            android:duration="150"
            android:propertyName="elevation"
            android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
    
    <item android:state_enabled="true">
        <objectAnimator
            android:duration="150"
            android:propertyName="elevation"
            android:valueTo="0dp"
            android:valueType="floatType" />
    </item>
    
    <item>
        <objectAnimator
            android:duration="0"
            android:propertyName="elevation"
            android:valueTo="0"
            android:valueType="floatType" />
    </item>
    

    android.valueTo="0dp" defines the elevation.

  2. Inflate and set the StateListAnimator to appBar

    val stateAnimator=AnimatorInflater.loadStateListAnimator(this,R.animator.appbar_animator);
    appBar.stateListAnimator=stateAnimator
    

StateListAnimator was added in api 21 Lollipop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.