2

I'm using the new material component like MaterialButton and MaterialCardView.
in my project, I need to change the material button tintBackground programmatically.
so I use setBackgroundTintList method to change the tint background color.

btnOk.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#20" + colorAccept)));  
btnOk.setTextColor(Color.parseColor("#" + colorAccept));

as you can see I'm setting a transparent color to my material button.


I run my app in android KitKat and there was no problem as you can see in this picture.

KitKat OutPut

but in Android Marshmallow, the material button Appearance changes and a shadow appear below the material button like below picture.

Marshmallow OutPut

I try some other code but none of them works.

  • first code

using below code does not change the tint background color of the button in android marshmallow.

ColorStateList colorOk = new ColorStateList(
                new int[][]{
                        new int[]{R.attr.buttonTint}
                },
                new int[] {
                        Color.parseColor("#20" + colorAccept)
                });  
  • second code

this code works just in KitKat and a shadow appears again in Marshmallow!

Drawable buttonDrawable = button.getBackground();
buttonDrawable = DrawableCompat.wrap(buttonDrawable);
//the color is a direct color int and not a color resource
DrawableCompat.setTint(buttonDrawable, Color.RED);
button.setBackground(buttonDrawable);

what is the problem that this shadow is showing in the newer API?

hassan moradnezhad
  • 455
  • 2
  • 6
  • 26

2 Answers2

4

this shadow appears in devices that use API version 21 or higher.
adding the below attribute in the XML file make the button without shadow.

android:stateListAnimator="@null"

attribute stateListAnimator is only used in API version 21 or higher

hassan moradnezhad
  • 455
  • 2
  • 6
  • 26
0

Try setting elevation

setElevation(float elevation)

MaterialButton materialButton =findViewById(R.id.buttonid);
materialButton.setElevation(0.0f);
Manoj Perumarath
  • 9,337
  • 8
  • 56
  • 77