android set button background programmatically

前端 未结 8 726
天涯浪人
天涯浪人 2020-11-30 02:57

I would like to know how to set the button color programatically? I have coded the following but fails:

Button11.setBackgroundColor(R.color.red);


        
相关标签:
8条回答
  • 2020-11-30 03:23

    Using setBackgroundColor() affects the style. So, declare a new style of the same properties with respect to the previous button, with a a different color.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/green"/>
    <corners android:radius="10dp"/>
    </shape>
    

    Now, use OnClick method.

    location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                location.setBackgroundResource(R.drawable.green);
    
            }
        });
    

    this changes the button but looks similar to changing the background.

    0 讨论(0)
  • 2020-11-30 03:26

    The answer you're looking for in 2020 and beyond:

    • setColorFilter(color, mode) is deprecated since API 29 (as discussed here)
    • button.setBackgroundColor(color) messes with the button style

    Now the proper way to set a buttons color is using BlendModeColorFilter() (see documentation).

    Usage:

    btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
    

    If you work with older APIs too:

    fun setButtonColor(btn: Button, color: Int) {
        if (Build.VERSION.SDK_INT >= 29)
            btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
        else
            btn.background.setColorFilter(color, PorterDuff.Mode.MULTIPLY)
    }
    

    Please vote to help others finding this answer - it took me quite a while figuring this out ^^

    0 讨论(0)
  • 2020-11-30 03:29

    You can set your desired color to the button programmatically like:

    Button11.setBackgroundColor(Android.Graphics.Color.parseColor("#738b28"));
    

    Also you can give the text color for a button like:

    Button11.setTextColor(Android.Graphics.Color.parseColor("#FFFFFF"));
    
    0 讨论(0)
  • 2020-11-30 03:30

    I have found that Android Studio gives me a warning that getColor() is deprecated when trying to do this:

    Button11.setBackgroundColor(getResources().getColor(R.color.red))
    

    So I found doing the method below to be the simple, up-to-date solution:

    Button11.setBackgroundColor(ContextCompat.getColor(context, R.color.red))
    

    You want to avoid hard-coding in the color argument, as it is considered bad code style.

    Edit: After using setBackgroundColor() with my own button, I saw that the internal button padding expanded. I couldn't find any way of changing it back to having both height and width set to "wrap_content". Maybe its a bug.

    Source: https://stackoverflow.com/a/32202256/6030520

    0 讨论(0)
  • 2020-11-30 03:44
    button.setBackgroundColor(getResources().getColor(R.color.red);
    

    Sets the background color for this view. Parameters: color the color of the background

    R.color.red is a reference generated at the compilation in gen.

    0 讨论(0)
  • 2020-11-30 03:45

    For not changing the size of button on setting the background color:

    button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
    

    this didn't change the size of the button and works with the old android versions too.

    0 讨论(0)
提交回复
热议问题