229

I want to remove the shadow from the button to make it seem more flat.

I have this right now:

cancel button with shadow

But I want this:

cancel button without shadow

Wormbo
  • 4,978
  • 2
  • 21
  • 41
sousheel
  • 2,419
  • 2
  • 13
  • 8
  • I write comment here, it's work for me. [http://stackoverflow.com/questions/27867284/remove-shadow-effect-on-android-button/30856094#30856094][1] [1]: http://stackoverflow.com/questions/27867284/remove-shadow-effect-on-android-button/30856094#30856094 – msamardzic Jun 15 '15 at 22:40
  • http://stackoverflow.com/questions/27408240/cant-get-rid-of-shadow-under-android-button – smileVann Jun 18 '15 at 08:58

16 Answers16

483

Another alternative is to add

style="?android:attr/borderlessButtonStyle"

to your Button xml as documented here http://developer.android.com/guide/topics/ui/controls/button.html

An example would be

<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
style="?android:attr/borderlessButtonStyle" />
Alt-Cat
  • 5,392
  • 1
  • 15
  • 16
233

A simpler way to do is adding this tag to your button:

android:stateListAnimator="@null"

though it requires API level 21 or more..

Alon Kogan
  • 3,258
  • 1
  • 21
  • 20
118

Kotlin

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
Michael
  • 9,639
  • 3
  • 64
  • 69
63

I use a custom style

<style name="MyButtonStyle" parent="@style/Widget.AppCompat.Button.Borderless"></style>

Don't forget to add

<item name="android:textAllCaps">false</item>

otherwise the button text will be in UpperCase.

Sam
  • 446
  • 7
  • 17
Zero
  • 2,764
  • 1
  • 17
  • 20
36

Material desing buttons add to button xml: style="@style/Widget.MaterialComponents.Button.UnelevatedButton"

clauub
  • 1,134
  • 9
  • 19
Efi G
  • 937
  • 8
  • 15
  • Sometimes it is similar to https://stackoverflow.com/a/38004981/2914140, but a gray button in disabled state becomes lighter. – CoolMind Aug 01 '19 at 09:57
27

try : android:stateListAnimator="@null"

Seth
  • 845
  • 8
  • 17
  • 9
    Although this may solve the OP’s problem, I’d recommend to add some context to it. Why will it help? Also, “try this” is a bit misleading. Are you sure it will solve the problem, or just guessing? If so, you should write a comment instead. – GergelyPolonkai Aug 03 '16 at 10:55
  • It requires API 21. – CoolMind Nov 21 '19 at 10:36
16

It requires API level 21

        android:outlineProvider="none"
Ronald Saunfe
  • 561
  • 6
  • 20
6

Using this as the background for your button might help, change the color to your needs

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_light" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/app_theme_dark" />
            <padding
                android:left="8dp"
                android:top="4dp"
                android:right="8dp"
                android:bottom="4dp" />
        </shape>
    </item>
</selector>
dlohani
  • 2,511
  • 16
  • 21
4

All above answers are great, but I will suggest an other option:

<style name="FlatButtonStyle" parent="Base.Widget.AppCompat.Button">
 <item name="android:stateListAnimator">@null</item>
 <!-- more style custom here --> 
</style>


Wilson Tran
  • 4,050
  • 3
  • 22
  • 31
3

the @Alt-Cat answer work for me!

R.attr.borderlessButtonStyle doesn't contain shadow.

and the document of button is great.

Also, you can set this style on your custom button, in second constructor.

    public CustomButton(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.borderlessButtonStyle);
    }
Xiang Li
  • 31
  • 1
2

Instead of a Button, you can use a TextView and add a click listener in the java code.

I.e.

in the activity layout xml:

<TextView
    android:id="@+id/btn_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimaryDark"
    android:text="@string/btn_text"
    android:gravity="center"
    android:textColor="@color/colorAccent"
    android:fontFamily="sans-serif-medium"
    android:textAllCaps="true" />

in the activity java file:

TextView btnTextView = (TextView) findViewById(R.id.btn_text_view);
btnTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // handler code
            }
        });
caitcoo0odes
  • 484
  • 1
  • 6
  • 17
  • This isn't the correct answer, but you actually helped me to fix my issue with a similar problem, so thank you! – box Sep 06 '17 at 10:16
1

Simply

 android:elevation="0dp"
Kipruto
  • 721
  • 6
  • 16
1

Update for Material Components

If you've previously used something like this:

<style name="MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:buttonStyle">@style/NoShadowButton</item>
</style>

<style name="NoShadowButton" parent="Widget.AppCompat.Button">
    <item name="android:stateListAnimator">@null</item>
</style>

And like me you're scratching your head wondering why it doesn't work anymore now that you've migrated to Theme.MaterialComponents.Light.*, it's because buttonStyle refers only to the AppCompat button style - there is a new style for materialButtonStyle. This fixed the issue for me:

<style name="MyTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="materialButtonStyle">@style/Widget.MaterialComponents.Button.UnelevatedButton</item>
</style>

Here's a good guide on what else has changed, and how to migrate properly.

Breeno
  • 3,007
  • 2
  • 31
  • 30
0

With a custom button; use the style R.style.Widget_AppCompat_Button_Borderless, Kotlin way-

class CSButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = R.style.Widget_AppCompat_Button_Borderless
) : AppCompatButton(context, attrs, defStyleAttr)
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
0

Not mentioned here before, but if you want to have a Material Button without shadow you can achieve it by creating following custom style:

<style name="materialButtonNoShadow" parent="Widget.MaterialComponents.Button.UnelevatedButton">

And then just apply it in your xml layout:

            <com.google.android.material.button.MaterialButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/noShadowButton"
            style="?attr/materialButtonNoShadow"/>
Vkalns86
  • 1
  • 2
0

For me android:outlineProvider="none" did the job.

ivan8m8
  • 388
  • 4
  • 17