Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 661 Vote(s) - 3.44 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to change background Color to MaterialButton from android.support.design programmatically

#1
I try change color to MaterialButton with this code:

var materialButton = findViewByid(R.id....) as MaterialButton
materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));

With a normal Button, it works.
Anyone have any ideas?
Reply

#2
You should change your colorAccent to change background for your materialButton.

Do not use the android:background attribute. 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.

For filled buttons, this class uses your theme's ?attr/colorAccent for the background tint color and white for the text color. For unfilled buttons, this class uses ?attr/colorAccent for the text color and transparent for the background tint.
Reply

#3
Lets try this:


mButton.getBackground().setColorFilter(getResources().getColor(R.color.indian_red), PorterDuff.Mode.SRC_ATOP);
Reply

#4
Simply call this attribute in the XML file within Material Button.

android:backgroundTint=""
Reply

#5
Try this

Drawable drawable = DrawableCompat.wrap(mButton.getBackground());
drawable.mutate();
DrawableCompat.setTint(drawable, getResources().getColor(R.color.my_color));
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
Reply

#6
I found the answer which is as below:

I changed the following line

materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));

to

materialButton.setBackgroundTintList(ContextCompat.getColorStateList(this@MyActivity, R.color.myCustomColor));

I tested it that way and it worked perfectly.
Reply

#7
materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor));
Reply

#8
`materialButton.setBackgroundColor( ContextCompat.getColor(this@MyActivity, R.color.myCustomColor))`

Working with material design dependency
`implementation 'com.google.android.material:material:1.2.0-alpha05'`
Reply

#9
For C#/Xamarin.Android:

button.BackgroundTintList = ColorStateList.ValueOf(element.Color.ToAndroid());
Reply

#10
**XML**

<com.google.android.material.button.MaterialButton
android:id="@+id/extended_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/button" />

Java



int integerValue = 0XFFC0C0C0;
extendedBtn.setBackgroundTintList(ColorStateList.valueOf(backgroundTint));

Kotlin

val integerValue = 0x00000000;
button.backgroundTintList = ColorStateList.valueOf(altColor);

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through