Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove action bar shadow programmatically

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.

like image 562
John Avatar asked Nov 12 '13 06:11

John


People also ask

How do I turn off Shadow Bar?

Use attribute app:elevation="0dp" to your Toolbar or AppBarLayout to remove the shadow. #. If you are using Toolbar only, then add attribute app:elevation="0dp" to Toolbar .

How do I get rid of the shadow bar on my Android?

By default, android provides shadow for action bar. This example demonstrates How to remove shadow below the action bar. Step 1 - Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 - Add the following code to res/layout/activity_main.


1 Answers

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);

    //...
}
like image 184
erakitin Avatar answered Sep 18 '22 18:09

erakitin