How to remove button shadow (android)

AndroidButtonShadow

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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" />

Solution 2 - Android

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

android:stateListAnimator="@null"

though it requires API level 21 or more..

Solution 3 - Android

Kotlin

stateListAnimator = null

Java

setStateListAnimator(null);

XML

android:stateListAnimator="@null"

Solution 4 - Android

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.

Solution 5 - Android

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

Solution 6 - Android

try : android:stateListAnimator="@null"

Solution 7 - Android

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>

Solution 8 - Android

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>

Solution 9 - Android

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

Solution 10 - Android

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

Solution 11 - Android

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)

Solution 12 - Android

Simply

 android:elevation="0dp"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsousheelView Question on Stackoverflow
Solution 1 - AndroidAlt-CatView Answer on Stackoverflow
Solution 2 - AndroidAlon KoganView Answer on Stackoverflow
Solution 3 - AndroidMichaelView Answer on Stackoverflow
Solution 4 - AndroidZeroView Answer on Stackoverflow
Solution 5 - AndroidEfi GView Answer on Stackoverflow
Solution 6 - AndroidSethView Answer on Stackoverflow
Solution 7 - AndroiddlohaniView Answer on Stackoverflow
Solution 8 - AndroidWilson TranView Answer on Stackoverflow
Solution 9 - AndroidXiang LiView Answer on Stackoverflow
Solution 10 - Androidcaitcoo0odesView Answer on Stackoverflow
Solution 11 - AndroidAnoop M MaddasseriView Answer on Stackoverflow
Solution 12 - AndroidKiprutoView Answer on Stackoverflow