question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[MaterialButton] Can't set background color for it

See original GitHub issue

Description: Normally, we initialize a button background by android:background="@color/dark_orange", however, in my case, it can not change the color into orange, it always be white color.

Expected behavior: The button color should be orange, but it always be white. Also, i print it value of color like this: Log.e(TIMEORY_GLOBAL_TAG, "The color is:${goTravelBtn.background}"), and the log result is:

2019-10-25 11:32:04.946 11308-11308/com.xxx.timeory E/Timeory GLOBAL_LOG: The color is:android.graphics.drawable.RippleDrawable@bdad964

Source code:

<com.google.android.material.button.MaterialButton
        android:id="@+id/goTravel"
        android:layout_width="280dp"
        android:layout_height="54dp"
        android:layout_marginBottom="36dp"
        android:background="@color/dark_orange"
        android:gravity="center"
        android:textSize="@dimen/text_size_normal"
        android:text="Go travelling"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

Android API version: 29

Material Library version: 1.2.0-alpha-01

Device: Pixel 2 API 29

To help us triage faster, please check to make sure you are using the latest version of the library.

We also happily accept pull requests.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
Moosphancommented, Oct 25, 2019

@MGaetan89 That’s right. I will use AppCompatButton instead, this widget is not effected by Materials 😃

0reactions
engbassemwaheedcommented, Feb 26, 2022

I had the same problem, and here is what it did: create a new style with a parent of a style of on of the material button styles and change the background color and background tint in it… hopefully it will work with you…

   <style name="DialogMaterialButtonOkay" parent="Widget.Material3.Button.UnelevatedButton">
        <item name="background">@color/button_background_color_main</item>
        <item name="backgroundTint">@color/button_background_color_main</item>
   </style>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't change background color on MaterialButton without ...
1st Solution. You can use app:backgroundTint to change back ground color of MaterialButton <com.google.android.material.button.
Read more >
MaterialButton - Android Developers
For unfilled buttons, this class uses ?attr/colorPrimary for the text color and transparent for the background tint. Add icons to the start, ...
Read more >
How to Change the Background Color of Button in Android ...
Go to the layout folder and in the activity_main.xml file change the ConstraintLayout to LinearLayout and give its orientation vertical.
Read more >
How to change background Color to MaterialButton from ...
I found the answer which is as below: I changed the following line materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.
Read more >
Android Button Background Color Not changing - YouTube
Tech Bholi presents how to solve android studio button background color not changing problem easily. Created by: Bholi AnsariEdited by: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found