How to remove Backgroundsource from button in Android

Andreas ss 726 Reputation points
2022-07-10T23:36:10.393+00:00

Hello,

I have a little problem to Remove a Resource.Drawable.borderGREEN_RED from a button in android.
I will tell what I do step by step:

  1. I SetBackgroundResource(ResRource.Drawable.borderGREEN_RED); to the button which works.
    The button is painted RED and sets a GREEN border. This works great!
  2. Now I need to somehow Remove this Resource.Drawable.borderGREEN_RED from the button so
    it has the state as it was before I did set it. I am trying to do that with:
    button1.SetBackgroundResource(0);
  3. The whole problem comes here. What I need to do in this step is to SetColorFilter on the button
    like this to make it completely yellow:
    button1.Background.SetColorFilter(Android.Graphics.Color.Yellow, Android.Graphics.PorterDuff.Mode.Multiply);

The problem on step 3 is that the .Background property is NULL because of what I did in step 2
where I put: button1.SetBackgroundResource(0);

My question would be. How do I remove the BackgroundResource that I put in step 1 so I can SetColorFilter in step 3?

Thank you!

                                Android.Views.View view = LayoutInflater.Inflate(Resource.Layout.Main, null);  
                                Button button1 = view.FindViewById<Button>(Resource.Id.button10);  
  
                                button1.SetBackgroundResource(Resource.Drawable.borderGREEN_RED); //STEP 1   
                                button1.SetBackgroundResource(0); //STEP 2   
                                button1.Background.SetColorFilter(Android.Graphics.Color.Yellow, Android.Graphics.PorterDuff.Mode.Multiply); //STEP 3  
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,378 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 79,706 Reputation points Microsoft Vendor
    2022-07-11T05:16:41.767+00:00

    Hello,​

    How do I remove the BackgroundResource that I put in step 1 so I can SetColorFilter in step 3?

    If you set SetBackgroundResource(0); //STEP 2 , Button do not have any background. The value of Background property is NULL in step 3.

    Remove this Resource.Drawable.borderGREEN_RED from the button so, it has the state as it was before I did set it

    You can get the Background Drawable before you execute step2. Then you can set it in Step 2 like following code.

       Button  button1 = FindViewById<Button>(Resource.Id.button10);  
       //get  state as it was before you did set it  
       Android.Graphics.Drawables.Drawable  btnDrawable = button1.Background;  
         
       button1.SetBackgroundResource(Resource.Drawable.borderGREEN_RED); //STEP 1   
         
       //STEP 2: set the state as it was before you did set it  
         
                   button1.Background=btnDrawable; //STEP 2  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.