Change button color when click = Gree when drop back normal color?

Asked

Viewed 129 times

0

I want when I click on Button it to change color and when I drop it back to original color!

Part of Button

public void addButton(String comprar, final InterfaceBtn interfaceBtn) {
    final Button button = new Button(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -1);
    layoutParams.setMargins(7, 5, 7, 5);
    button.setLayoutParams(layoutParams);
    button.setPadding(10, 5, 10, 5);
    button.setTextSize(13.0f);
    button.setTextColor(Color.parseColor("#D5E3EB"));
    button.setGravity(17);

    if (comprar.contains("OnOff_")) {
        comprar = comprar.replace("OnOff_", "");
        button.setText(comprar + ": OFF");
        button.setBackgroundColor(Color.parseColor("#7f0000"));
        final String comprar2 = comprar;
        button.setOnClickListener(new View.OnClickListener() {
            private boolean isActive = true;

            public void onClick(View v) {
                interfaceBtn.OnWrite();
                if (isActive) {
                    playSound(Uri.fromFile(new File(cacheDir + "On.ogg")));
                    button.setText(comprar2 + ": ON");
                    button.setBackgroundColor(Color.parseColor("#003300"));
                    isActive = false;
                    return;
                }
                playSound(Uri.fromFile(new File(cacheDir + "Off.ogg")));
                button.setText(comprar2 + ": OFF");
                button.setBackgroundColor(Color.parseColor("#7f0000"));
                isActive = true;
            }
        });
    } else {
        button.setText(comprar);
        button.setBackgroundColor(Color.parseColor("#1C262D"));
        final String comprar2 = comprar;
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                interfaceBtn.OnWrite();
                playSound(Uri.fromFile(new File(cacheDir + "Select.ogg")));
            }
        });
    }
    patches.addView(button);
}

The if part is already the way I want it! When I click the active button and it turns green On, when I click again it turns off and turns red Off.

now the second that and Else can not. I want to click it change color is to release it back to default color.

That part I mentioned //button.setBackgroundColor(Color.parseColor("#8A2BE2")); When I click it changes color, but when I drop it does not return to default color!

1 answer

0


As you are looking for something direct in the code what happens is the following change in this method Onclick occurs when Oce releases the button, example the button is in the color 1 Oce presses it and when release happens the action so it does not return to color 1 because not of time. so I suggest the following put a Reader to listen when you release the button and another to when you press, I won’t even talk about performance here I’m just giving a hint to solve the problem the code that follows demonstrates how it works:

public class MainActivity extends AppCompatActivity {
Button botao;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    botao=findViewById(R.id.button);
    botao.setBackgroundColor(getResources().getColor(R.color.cor1));

    botao.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            botao.setBackgroundColor(getResources().getColor(R.color.cor2));
            return false;
        }
    });

    botao.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           botao.setBackgroundColor(getResources().getColor(R.color.cor1));

        }
    });


}

}

now applying to your code:

public void addButton(String comprar, final InterfaceBtn interfaceBtn) {




final Button button = new Button(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -1);
layoutParams.setMargins(7, 5, 7, 5);
button.setLayoutParams(layoutParams);
button.setPadding(10, 5, 10, 5);
button.setTextSize(13.0f);
button.setTextColor(Color.parseColor("#D5E3EB"));
button.setGravity(17);

if (comprar.contains("OnOff_")) {
    comprar = comprar.replace("OnOff_", "");
    button.setText(comprar + ": OFF");
    button.setBackgroundColor(Color.parseColor("#7f0000"));
    final String comprar2 = comprar;
    button.setOnClickListener(new View.OnClickListener() {
        private boolean isActive = true;

        public void onClick(View v) {
            interfaceBtn.OnWrite();
            if (isActive) {
                playSound(Uri.fromFile(new File(cacheDir + "On.ogg")));
                button.setText(comprar2 + ": ON");
                button.setBackgroundColor(Color.parseColor("#003300"));
                isActive = false;
                return;
            }
            playSound(Uri.fromFile(new File(cacheDir + "Off.ogg")));
            button.setText(comprar2 + ": OFF");
            button.setBackgroundColor(Color.parseColor("#7f0000"));
            isActive = true;
        }
    });
} else {
    button.setText(comprar);
     button.setBackgroundColor(Color.parseColor("#7f0000"));
    final String comprar2 = comprar;

    button.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
           button.setBackgroundColor(Color.parseColor("#003300"));
           interfaceBtn.OnWrite();
            playSound(Uri.fromFile(new File(cacheDir + "Select.ogg")));
            return false;
        }
    });



    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
       button.setText(comprar); 
       final String comprar2 = comprar;
       button.setBackgroundColor(Color.parseColor("#7f0000"));
        }
    });
}
patches.addView(button);
}

Take the test.

  • Good morning! I was kind of lost to add the base you mentioned, because I’m still layman in that part. There are ways to use my code pro pio above?

  • I went over your code calmly and I had a question. You just posted Isis there, the question is the following, in the part of if you’re setting the Onclick on the button too?

  • If you can edit the question and post the if part I can edit the answer showing exactly how it should be done. It would make it easy enough

  • Ready the whole part of the button is up there, and the bottom of the code has what I want to do well explained.

  • In this part I added the color I want, when clicking it changes the color but when I release the button it continues with the same color button.setOnClickListener(new View.Onclicklistener() { public void onClick(View v) { //after clicking will happen this button.setBackgroundColor(Color.parseColor("#8A2BE2")); ///but I’m not sure I got the color and the name in it ////before clicking&#xTo; } });

  • I modified the code in the answer copy and run and see what happens

  • I checked and put a reply there, thank you very much!

  • even if it worked I suggest you study a more efficient way to make this change in the button.

  • There are other ways to do it, but it wasn’t working in my code.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.