Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting backgroundTint on Material Button with alpha has a weird visual effect in normal and pressed state

I'm using Material Buttons in my project and trying to set backgroundTint with alpha value.

<!-- background_tint.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@android:color/black" android:alpha="0.60"/>
</selector>

<!-- activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.button.MaterialButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:text="Sign in"
        app:backgroundTint="@color/background_tint" />
</LinearLayout>

The resulting button however looks weird while in normal state and even weirder while pressed.
enter image description here
I don't see this issue when I set backgroundTint to specific shade of gray such as #777777. Why does this happen with alpha value?

like image 270
Abhishek Avatar asked Jun 11 '19 01:06

Abhishek


People also ask

How do you make a button background transparent?

CSS Code: In this section, we will design the button using CSS property. We will use the background-color: transparent; property to set the button with transparent look. Complete Code: In this section, we will combine the above two sections to create a transparent background button.

How change material button background color in android programmatically?

I try change color to MaterialButton with this code: var materialButton = findViewByid(R.id....) as MaterialButton materialButton. setBackgroundColor( ContextCompat. getColor(this@MyActivity, R.

What is Backgroundtint?

“what is backgroundtint in android” Code Answer Friends, Background Tint Mode in android studio is use to down the background color and you can add , multiply , opacity and something else mode use to color overlapping on any background , and its support only API Level 5.0 and upper version.

Do not set the background MaterialButton manages its own background drawable?

MaterialButton manages its own background drawable, and setting a new background means MaterialButton can no longer guarantee that the new attributes it introduces will function properly. If the default background is changed, MaterialButton cannot guarantee well-defined behavior.


2 Answers

We had the same issue with partially opaque elevated material views.

Weird octagonal shadow artifacts

Simply adding android:stateListAnimator="@null" removed the visible shadow artifacts.

We also set the elevation to 0dp which I don't think is required.

like image 123
ElliotM Avatar answered Oct 13 '22 20:10

ElliotM


You just need to change the style property of Material Button.

style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
like image 28
Developer X Avatar answered Oct 13 '22 20:10

Developer X