1

I currently have this bit of code, but I need to repeat it for red green and blue, is there a way I can do it without copying and pasting the code 3 times??

    yellow.setOnClickListener(new View.OnClickListener() {    
        public void onClick (View v)   {   

            switch (buttonCount) {
            case 1:  
                empty1.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess1= Colour.YELLOW;
                break;
            case 2:  
                empty2.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess2=Colour.YELLOW;
                break;
            case 3:  
                empty3.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess3=Colour.YELLOW;
                break;
            case 4:  
                empty4.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess4=Colour.YELLOW;
                break;
            case 5:  
                empty5.setImageResource(R.drawable.yellow);
                buttonCount++;
                guess5=Colour.YELLOW;
                break;

            }
        }
    });
2
  • 3
    Write a method for it and call it? Where are you stuck at? Commented Nov 23, 2013 at 19:28
  • I don't know the syntax.. I understand what you're saying I just don't understand how to write it. Commented Nov 23, 2013 at 19:37

3 Answers 3

1

You can easily abstract this out by checking which button is clicked and then set the appropriate image from that, heres a quick examle (though likely not working code depending on your id's):

public void onClick (View v)   {   
    int drawable = 0;
    Colour guess = null;
    switch (v.getId())
    {
        case R.id.yellowButton:
            drawable = drawable;
            guess = Colour.YELLOW;
            break;
        case R.id.blueButton:
            drawable = R.drawable.blue;
            guess = Colour.BLUE
            break;
        case R.id.redButton:
            drawable = R.drawable.red;
            guess = Colour.RED
            break;
        case R.id.greenButton:
            drawable = R.drawable.green;
            guess = Colour.GREEN
            break;
    }
    switch (buttonCount) 
    {
    case 1:  
        empty1.setImageResource(drawable);
        buttonCount++;
        guess1= guess;
        break;
    case 2:  
        empty2.setImageResource(drawable);
        buttonCount++;
        guess2=guess;
        break;
    case 3:  
        empty3.setImageResource(drawable);
        buttonCount++;
        guess3=guess;
        break;
    case 4:  
        empty4.setImageResource(drawable);
        buttonCount++;
        guess4=guess;
        break;
    case 5:  
        empty5.setImageResource(drawable);
        buttonCount++;
        guess5=guess;
        break;

    }
}

then you can simply just register the buttons to this onClick method (either through xml or programmatically)

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know much android but you can define a private a class

private class MyClickListener implements View.OnClickListener{

   private final String colour;
   private final String imageResource;

    public MyClickListener(String colour, String imageResource){
      this.colour = colour;
      this imageResource = imageResource;
    }   

   @Override
   public void onClick (View v)   {   

            switch (buttonCount) {
            case 1:  
                empty1.setImageResource(imageResource);
                buttonCount++;
                guess1= colour;
                break;
            case 2:  
                empty2.setImageResource(imageResource);
                buttonCount++;
                guess2=colour;
                break;
            case 3:  
                empty3.setImageResource(imageResource);
                buttonCount++;
                guess3=colour;
                break;
            case 4:  
                empty4.setImageResource(imageResource);
                buttonCount++;
                guess4=colour;
                break;
            case 5:  
                empty5.setImageResource(imageResource);
                buttonCount++;
                guess5=colour;
                break;

            }
        }
}

And in your client code

yellow.setOnClickListener(new View.OnClickListener(new MyClickListener(Colour.YELLOW));

Also instead of using a lot of variables of the same type as empty1 empty2 you can put them in a collection like a List preferable an ArrayList, the same with guess1 guessN then in your code

            list.get(buttonCount).setImageResource(imageResource);
            list.set(buttonCount,colour);
            buttonCount++;

1 Comment

The R.drawable.yellow probably needs to be parametrized as well.
0

You CAN use reflections to do this, but you should be aware of the dangers the reflections bring. Anyway, your code would look like this:

  Button button;
  Class c = Class.forName("your.package.name.R$id");
  Field field = c.getDeclaredField("empty"+buttonCount);
  button = (Button) findViewById(field.getInt(null));
  button.setImageResource(R.drawable.yellow);

Surround it with all the exception necessary.

EDIT: In line: c.getDeclaredField("empty"+buttonCount); you have to use the actual id by which they were declared in your layout xml. I assued it was "empty1", "empty2"... "empty5"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.