0

I simply want to change the backgroundTint of a normal button from inside the java code. I tried many different approaches like ColorStateList or setColorFilter, but nothing worked. I am purposefully not using setBackgroundColor since I want to keep the original shape of the button.

Furthermore, the colors I want to use are already defined in my resources. After lots of trial and error I managed to access these colors with this code:

int colorBtnDeactivated = ContextCompat.getColor(this, R.color.colorBtnDeactivated);

So basically I only need this one line of java code which enables me to access the background tint. The rest I can do myself.

I would really appreciate help, I have been stuck on this problem for hours. Thanks!

Edit: Using a selector-xlm didn't work, since it only changed the color of the button while being pressed. Also the buttons will influence each other, so by pressing one button I will need to be able to change the background tint of another button.

Edit 2: I tried again with setColorFilter:

//this is all inside the onCreate-method

int colorBtnActiveTest= ContextCompat.getColor(this, colorBtnActive);
int colorBtnDeactivatedTest=ContextCompat.getColor(this, colorBtnDeactivated);

Button btnKnockOne = (Button)findViewById(R.id.btnKnockOne);
boolean stateBtnKnockOne = false;
btnKnockOne.getBackground().setColorFilter(colorBtnDeactivatedTest, PorterDuff.Mode.SRC_IN);

btnKnockOne.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stateBtnKnockOne==false){
                    btnKnockOne.getBackground().setColorFilter(colorBtnActiveTest, PorterDuff.Mode.SRC_IN);
                    stateBtnKnockOne=true;
                }

                else if (stateBtnKnockOne==true){
                    btnKnockOne.getBackground().setColorFilter(colorBtnDeactivatedTest, PorterDuff.Mode.SRC_IN);
                    stateBtnKnockOne=false;
                }
            }
        });

This is the result:

  • When I open the activity, the button is displayed in the default grey button color, not in my custom color colorBtnDeactivatedTest
  • When I press the button, it briefly changes its color to colorBtnActiveTest, but then goes back to its grey color
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
newman
  • 75
  • 1
  • 8
  • check this https://stackoverflow.com/questions/20121938/how-to-set-tint-for-an-image-view-programmatically-in-android – Kuti Gbolahan Nov 16 '17 at 16:18
  • i tried this, but changing the PorterDuff.Mode never showed the desired result. I will try it again and post my code. – newman Nov 16 '17 at 16:22

1 Answers1

0

I finally found a solution! This post helped me find it: https://stackoverflow.com/a/8748112/8952749

Now I basically have two buttons of which only one can be selected at one time. For this to work I created a selector-xlm:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:state_selected="true"
        android:drawable="@drawable/button_active" />
    <item
        android:state_selected="false"
        android:drawable="@drawable/button_deactivated" />

</selector>

This is the java code, which enables me to change state_selected of the buttons:

    stateBtn1=false;
    stateBtn2=false;

    btnTest1=(Button)findViewById(R.id.button);
    btnTest2=(Button)findViewById(R.id.button2);

    btnTest1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (stateBtn1==false){
                btnTest1.setSelected(true);
                stateBtn1=true;

                btnTest2.setSelected(false);
                stateBtn2=false;
            }
            else if (stateBtn1==true){
                btnTest1.setSelected(false);
                stateBtn1=false;
            }
        }
    });

    btnTest2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (stateBtn2==false){
                btnTest2.setSelected(true);
                stateBtn2=true;

                btnTest1.setSelected(false);
                stateBtn1=false;
            }
            else if (stateBtn2==true){
                btnTest2.setSelected(false);
                stateBtn2=false;
            }
        }
    });
newman
  • 75
  • 1
  • 8