I want to remove the shadow from the button to make it seem more flat.
I have this right now:
But I want this:
I want to remove the shadow from the button to make it seem more flat.
I have this right now:
But I want this:
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" />
A simpler way to do is adding this tag to your button:
android:stateListAnimator="@null"
though it requires API level 21 or more..
Kotlin
stateListAnimator = null
Java
setStateListAnimator(null);
XML
android:stateListAnimator="@null"
Material desing buttons add to button xml:
style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
try : android:stateListAnimator="@null"
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>
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>
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);
}
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
}
});
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.
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)
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"/>