0

I have 3 Buttons.

When i click on any of the Buttons, i want the color of the Button to change if the Button text matches with a String in a variable String ans;.

Can someone help me?

Here is what i have tried:

public void onClick(View v) {
        // TODO Auto-generated method stub
        if((btn10.getId())==(R.id.btn10))
        {
            if(btn10.getText().toString().equals(ans))
            {
                btn10.setBackgroundColor(Color.GREEN);
                score=score+10;


            }
            else
            {
                if((btn11.getId())==(R.id.btn11))
                {
                    btn10.setBackgroundColor(Color.RED);

                }
                if((btn12.getId())==(R.id.btn12))
                {
                    btn12.setBackgroundColor(Color.RED);

                }
                ch=ch-1;
            }
        }
        else if((btn11.getId())==(R.id.btn11))
        {
            if(btn11.getText().toString().equals(ans))
            {
                btn11.setBackgroundColor(Color.GREEN);
                score=score+10;
            }
            else
            {
                if((btn12.getId())==(R.id.btn12))
                {
                    btn12.setBackgroundColor(Color.RED);

                }
                if((btn10.getId())==(R.id.btn10))
                {
                    btn10.setBackgroundColor(Color.RED);

                }
                ch=ch-1;
            }
        }
        else if((btn12.getId())==(R.id.btn12))
        {
            if(btn12.getText().toString().equals(ans))
            {
                btn12.setBackgroundColor(Color.GREEN);
                score=score+10;
            }
            else
            {
                if((btn11.getId())==(R.id.btn11))
                {
                    btn11.setBackgroundColor(Color.RED);
                }
                if((btn10.getId())==(R.id.btn10))
                {
                    btn10.setBackgroundColor(Color.RED);

                }
                ch=ch-1;
            }
        }
    }

Any help is appreciated, thanks in advance.

prolink007
  • 33,872
  • 24
  • 117
  • 185
user1568346
  • 29
  • 1
  • 7

4 Answers4

2

First, put all your buttons in a collection,

Collection<Button> buttons = new HashSet<Button>();
buttons.add(findViewById(R.id.btn10);
buttons.add(findViewById(R.id.btn11);
...

If all the buttons are under a common container, you can do it like,

ViewGroup parent = findViewById(...);
for (int i = 0, l = parent.getChildCount(); i++) {
  Button b = (Button) parent.getChildAt(i);
  buttons.add(b);
}

Define an onClickListener to set the button color correctly,

View.OnClickListener ocl = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      for (Button b: buttons) {
        if (b.getText().toString().equals(ans)) {
          b.setBackgroundColor(Color.GREEN);
        } else {
          b.setBackgroundColor(Color.RED);
        }
      }    
    }
};

Set the onClickListener for each of the buttons,

for (Button b: buttons) {
  b.setOnClickListener(ocl);
}
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
  • thank u guys for the help...its working bt i also want if on the click text does nt match it goes red and the button having the correct text matching with string goes green automatically..... how can this b implemented?? – user1568346 Aug 01 '12 at 19:15
  • thanx for the code .... wht i tried n its working too is ...... i have fetch the button id for correct answer 1st and den performed the required action........thanx for the help – user1568346 Aug 05 '12 at 12:46
1

Read the APIs that are available for Button. This will help you solve your problem. Or you could just maintain the order of the Buttons via a Structure.. like a Hashmap or something, when one of the button clicks happens, then you retrieve it from the Datastructure and compare it with the answer.

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

This code might help you:

public void onClick(View v) {
switch(v.getId(){
case R.id.btn10:
if(v.getText().equals("ans")) {
v.setBackgroundColor(Color.RED);
} else {
v.setBackgroundColor(Color.Green);
}
break;
case R.id.btn11:
break;
case R.id.btn12:
break;
default:
break;
}
prolink007
  • 33,872
  • 24
  • 117
  • 185
Basbous
  • 3,927
  • 4
  • 34
  • 62
  • thank u guys for the help...its working bt i also want if on the click text does nt match it goes red and the button having the correct text matching with string goes green automatically..... how can this b implemented?? – user1568346 Aug 01 '12 at 19:16
0

Well you can find this thread very helpful with your problem, they write some tips that you may be missing in your implementation. like that "v" is a button, and actually is the button you pressed so reading that post would be a good start, good luck.

Community
  • 1
  • 1
Jorge Aguilar
  • 3,442
  • 30
  • 34