2
I’m developing an app where it will contain several buttons.
I need your help to see if I can improve the code.
Actions do the same thing just change the background color and pass me the button value.
The application has several other screens that have many buttons with almost the same actions, only with some changes in return.
My code:
btnBall01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count1++;
selectedNumber++;
games.setBallId(btnBall01.getId());
btnBall01.setBackgroundResource(R.mipmap.ball_verde);
games.setActualNumber(btnBall01.getText().toString());
Toast.makeText(ActivityMegasenaCreateGame.this, "Verde " + games.getActualNumber(), Toast.LENGTH_SHORT).show();
if (count1 != 1){
count1 = 0;
selectedNumber = selectedNumber -2;
btnBall01.setBackgroundResource(R.mipmap.ball);
Toast.makeText(ActivityMegasenaCreateGame.this, "Branco " + games.getActualNumber(), Toast.LENGTH_SHORT).show();
initializeComponentsBallsButtons();
}
}
});
btnBall02.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnBall02.setBackgroundResource(R.mipmap.ball_verde);
count2++;
selectedNumber++;
games.setBallId(btnBall02.getId());
games.setActualNumber(btnBall02.getText().toString());
if (count2 != 1){
count2 = 0;
selectedNumber = selectedNumber -2;
btnBall02.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall03.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall03.setBackgroundResource(R.mipmap.ball_verde);
count3++;
selectedNumber++;
games.setBallId(btnBall03.getId());
games.setActualNumber(btnBall03.getText().toString());
if (count3 !=1){
count3 = 0;
selectedNumber = selectedNumber -2;
btnBall03.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall04.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall04.setBackgroundResource(R.mipmap.ball_verde);
count4++;
selectedNumber++;
games.setBallId(btnBall04.getId());
games.setActualNumber(btnBall04.getText().toString());
if (count4 != 1){
count4 = 0;
selectedNumber = selectedNumber -2;
btnBall04.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall05.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall05.setBackgroundResource(R.mipmap.ball_verde);
count5++;
selectedNumber++;
games.setBallId(btnBall05.getId());
games.setActualNumber(btnBall05.getText().toString());
if (count5 != 1){
count5 = 0;
selectedNumber = selectedNumber -2;
btnBall05.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall06.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall06.setBackgroundResource(R.mipmap.ball_verde);
count6++;
selectedNumber++;
games.setBallId(btnBall06.getId());
games.setActualNumber(btnBall06.getText().toString());
if (count6 != 1){
count6 = 0;
selectedNumber = selectedNumber -2;
btnBall06.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall07.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall07.setBackgroundResource(R.mipmap.ball_verde);
count7++;
selectedNumber++;
games.setBallId(btnBall07.getId());
games.setActualNumber(btnBall07.getText().toString());
if (count7 != 1){
count7 = 0;
selectedNumber = selectedNumber -2;
btnBall07.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall08.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall08.setBackgroundResource(R.mipmap.ball_verde);
count8++;
selectedNumber++;
games.setBallId(btnBall08.getId());
games.setActualNumber(btnBall08.getText().toString());
if (count8 != 1){
count8 = 0;
selectedNumber = selectedNumber -2;
btnBall08.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall09.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall09.setBackgroundResource(R.mipmap.ball_verde);
count9++;
selectedNumber++;
games.setBallId(btnBall09.getId());
games.setActualNumber(btnBall09.getText().toString());
if (count9 != 1){
count9 = 0;
selectedNumber = selectedNumber -2;
btnBall09.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall10.setBackgroundResource(R.mipmap.ball_verde);
count10++;
selectedNumber++;
games.setBallId(btnBall10.getId());
games.setActualNumber(btnBall10.getText().toString());
if (count10 != 1){
count10 = 0;
selectedNumber = selectedNumber -2;
btnBall10.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall11.setBackgroundResource(R.mipmap.ball_verde);
count11++;
selectedNumber++;
games.setBallId(btnBall11.getId());
games.setActualNumber(btnBall11.getText().toString());
if (count11 != 1){
count11 = 0;
selectedNumber = selectedNumber -2;
btnBall11.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall12.setBackgroundResource(R.mipmap.ball_verde);
count12++;
selectedNumber++;
games.setBallId(btnBall12.getId());
games.setActualNumber(btnBall12.getText().toString());
if(count12 != 1){
count12 = 0;
selectedNumber = selectedNumber -2;
btnBall12.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall13.setBackgroundResource(R.mipmap.ball_verde);
count13++;
selectedNumber++;
games.setBallId(btnBall13.getId());
games.setActualNumber(btnBall13.getText().toString());
if(count13 != 1){
count13 = 0;
selectedNumber = selectedNumber -2;
btnBall13.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
btnBall14.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnBall14.setBackgroundResource(R.mipmap.ball_verde);
count14++;
selectedNumber++;
games.setBallId(btnBall14.getId());
games.setActualNumber(btnBall14.getText().toString());
if (count14 != 1){
count14 = 0;
selectedNumber = selectedNumber -2;
btnBall14.setBackgroundResource(R.mipmap.ball);
initializeComponentsBallsButtons();
}
}
});
Thank you very much, it worked!!
– Edi