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!
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?
– Henrique
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?
– juliano santos
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
– juliano santos
Ready the whole part of the button is up there, and the bottom of the code has what I want to do well explained.
– Henrique
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; } });
– Henrique
I modified the code in the answer copy and run and see what happens
– juliano santos
I checked and put a reply there, thank you very much!
– Henrique
even if it worked I suggest you study a more efficient way to make this change in the button.
– juliano santos
There are other ways to do it, but it wasn’t working in my code.
– Henrique