Home » Android » android – Styling a AppCompatButton in v21 to have no shadow and corner radius of zero

android – Styling a AppCompatButton in v21 to have no shadow and corner radius of zero

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a AppCompatButton defined in a XML layout, and I have set a theme for it like this:

android:theme="@style/CustomAccentOverlay"

And I have set:

android:stateListAnimator="@null"

to remove the shadow. I have two problems with this.

The height of the button is deducted the height of the shadow, even though the shadow is not shown. Should I remove the shadow in some other way, or how do I fix this?

The button has rounded corners, and I want the corners to be sharp. I can not set a background on the button, because I want to keep the standard ripple effect and that goes away if I set a background (at least I don’t know how to keep it if I set a background). I have tried setting

<item name="android:bottomLeftRadius">0dp</item>

and all the other corners to the CustomAccentOverlay theme and also its corresponding style, but it does not work. How can I set the corner radius to zero on my button?

Thank you
Søren

How to&Answers:

Use the following code for the Button.

<android.support.v7.widget.AppCompatButton
android:layout_width="200dp"
android:layout_height="200dp"
android:text="Button"
android:stateListAnimator="@null"
android:elevation="0dp"
android:background="@android:color/darker_gray"
android:foreground="?attr/selectableItemBackground"
/>

I will explain the attributes.

  1. android:elevation=”0dp” and android:stateListAnimator=”@null”. No shadow for the button.

  2. android:background . Set the desired color as background. It removes the rounded corners.

  3. android:foreground=”?attr/selectableItemBackground” . It gives the ripple effect when the button is pressed.

Update 1:

It seems like android:foreground attribute for View works from API 23. For below APIs, create a drawable with ripple in drawable-v21 folder and set it as background to the button,

<ripple
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="NewApi"
    android:color="@color/ripple_color">

    <item android:drawable="@color/normal_state_button_background_color"/>

</ripple>

For pre Lollipop versions, create a drawable with selector in drawable folder with the same name.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/pressed_color"
          android:state_pressed="true" />
    <item android:drawable="@drawable/focused_color"
          android:state_focused="true" />
    <item android:drawable="@drawable/normal_color" />
</selector>

Answer:

First question:How to remove shadow of a button?

Here is the answer:
Just add this attribute to your button

android:stateListAnimator="@null"

Second question: How to make the corner of button sharp without losing the standard ripple effect.
Here is the answer: But first you have to make two drawble file with same name but one for below api 21 and one for api > 21 because the ripple is only available only api > 21.So now I am showing how to create that.Read the following text carefully

Right click on the drawble folder and choose new and “Drawble resource file” and hit next then name the drawble whatever you like and press ok.Then again right click on the drawble folder and choose new and “Drawble resource file” and hit next and name the drawble exactly what you named the previous drawble folder but this time at the bottom you can see a section called “available qualifiers”.Go to this section and at the very bottom you can see “Version”,click it and then you can see a arrow icon at the right,click it then in the “Platform api level” add 21 and then press ok.And now if you expand drawble folder you can see two file of your created drawble file.Once for api that is below 21 and once for upper 21.Open drawble file that you have created and make sure you open that have “(v21)” at the last.Now delete everything from there and add the following code

<?xml version="1.0" encoding="utf-8"?>
<ripple android:color="?attr/colorControlHighlight" xmlns:android="http://schemas.android.com/apk/res/android">

<item>
    <shape>
        <corners android:radius="0dp"/>
        <solid android:color="#D6D7D7"/>
    </shape>
</item>

</ripple>

And add this attribute to your button

android:background="@drawable/youdrawblefilethatyouhavecreated"

And now if you run your application you can see that there is no shadow and your button has sharp corner and if you click the ripple shows up.

Lastly,your button look something like this

<android.support.v7.widget.AppCompatButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button 1"
    android:background="@drawable/yourcreatddrawblefile"
    android:stateListAnimator="@null"/>

Hope this help!

Answer:

It really sounds like you want to use a clickable TextView rather than a Button. TextView by default will not have shadow and has sharp corners and you can attach a click listener to it. Remember, Button is just a fancy TextView with a lot of visual add-ons to it, and it sounds like you want to remove a lot of it.

If you want to keep the ripple on the TextView and define your own background, set android:foreground="?attr/selectableItemBackground"

EDIT: Even though the other answer was marked accepted, I would still argue that the OP should use a TextView with a click listener and applying the ripple effects to that than using a Button. Clickable TextViews in this manner are exactly how the Google I/O app implements all of their flat buttons that meet Material Design spec.

Answer:

Use this code

 <android.support.v7.widget.AppCompatButton
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorAccent"
    android:text="@string/button" />