There is no component that does this for you similar to RadioGroup
. You have to do this via code, something like:
...
firstButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonState(true, false, false);
}
});
secondButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonState(false, true, false);
}
});
thirdButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeButtonState(false, false, true);
}
});
...
And to change the state of your Button
:
private void changeButtonState(boolean first, boolean second, boolean third) {
// caso você queira alterar a cor do botao selecionado
if (first) {
firstButton.setColorFilter(R.color.yourColor);
secondButton.setColorFilter(R.color.yourColor2);
thirdButton.setColorFilter(R.color.yourColor2);
} else if (second) {
firstButton.setColorFilter(R.color.yourColor2);
secondButton.setColorFilter(R.color.yourColor);
thirdButton.setColorFilter(R.color.yourColor2);
} else {
firstButton.setColorFilter(R.color.yourColor2);
iBtnRatingMedium.setColorFilter(R.color.yourColor2);
thirdButton.setColorFilter(R.color.yourColor);
}
firstButton.setSelected(first);
secondButton.setSelected(second);
thirdButton.setSelected(third);
}
Thank you!.. a question, the "setOnClickListener" where would you put it? Inside the Oncreate()?
– porthfind
@porthfind this, soon after instantiating its components
– rsicarelli
I put in xml the android command:onClick="onClick" on each button and dp in Java have there a method onClick...n know if it is correct to do so mt, but I do not think it is correct to put in onCreate()
– porthfind