2

I need to change my button background color to Red.

I try this button.SetBackgroundColor(Color.Red); but button gets bigger, then I try this button.BackgroundTintList = (ColorStateList.ValueOf(Color.Red)); and works well.

But after that I need to put background button as before and I can't do that. I have another button with same background and try to copy from there using this Button anotherButton = _v.FindViewById<Button>(Resource.Id.bt_anotherButton); button.Background = anotherButton.Background;

But instead of putting anotherButton background equals to button background, does the opposite and both button became Red.

Anyone could help?

MMachado
  • 88
  • 10
  • You can save the previous color . Or you can use a Selector drawable for multiple states. – ADM May 29 '18 at 15:56
  • I'm trying save previous background but I can't do that. I have this `Drawable dr = button.Background;` and then put dr as bacground but keeps red – MMachado May 29 '18 at 16:49

2 Answers2

1

Like @hichame has said, your button is not getting bigger. You can read this and this to understand why your button looks like bigger.

About the button's color, you can refer to this use android.R.drawable.btn_default:

button.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault);

to reset your button. But, the result is not like the origin button.


Finally, I get the default button's color :#D6D7D7.

You can use this value to reset your button's color to default:

button.Background.SetTintList(ColorStateList.ValueOf(Color.ParseColor("#D6D7D7")));

Update:

After read this answer, I find a solution, you need use AppCompatButton, install Xamarin.Android.Support.v7.AppCompat, use it in your layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
  <android.support.v7.widget.AppCompatButton
      android:id="@+id/bt1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
  <android.support.v7.widget.AppCompatButton
      android:id="@+id/bt2"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />
</LinearLayout>

Use SupportBackgroundTintList in your MainActivity( also that is why your return value is null):

public class MainActivity : AppCompatActivity,View.IOnClickListener
{
    AppCompatButton bt1;
    ColorStateList backgroundTintList;
    public void OnClick(View v)
    {
        bt1.SupportBackgroundTintList=(backgroundTintList);
        //bt1.SetBackgroundResource(Android.Resource.Drawable.ButtonDefault);
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        
        bt1 = FindViewById<AppCompatButton>(Resource.Id.bt1);
        AppCompatButton bt2 = FindViewById<AppCompatButton>(Resource.Id.bt2);
       
        backgroundTintList = bt2.SupportBackgroundTintList;

        bt1.SupportBackgroundTintList=ColorStateList.ValueOf(Color.Red);

        bt2.SetOnClickListener(this); 
       
    }
}

Result:

enter image description here

Community
  • 1
  • 1
Robbit
  • 4,300
  • 1
  • 13
  • 29
  • Thanks for answer. I try this `button.Background.SetTintList(ColorStateList.ValueOf(Color.ParseColor("#D6D7D7")));` and it works. Now I have this [code](https://pastebin.com/KcXx7wra) and works perfect. – MMachado May 30 '18 at 08:26
0

Your button is not getting bigger but it is getting the padding of the default Android button. You have to set the tint to not modify the size of your background settings and you can do it by:

var previousTint = button.BackgroundTintList;
button.Background.SetTint (Color.HoloRedDark);

And once done, set back the previous tint.

Hichame Yessou
  • 2,658
  • 2
  • 18
  • 30
  • Thanks for answer. This var previousTint = button.BackgroundTintList; returns null. Rigth now I have this: https://pastebin.com/du3jVfuL – MMachado May 29 '18 at 16:53