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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

Try setting elevation

setElevation(float elevation)

MaterialButton materialButton =findViewById(R.id.buttonid);
materialButton.setElevation(0.0f);

1 Comment

thanks for your answer but it does not work and shadow is showing :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.