user2456977
user2456977

Reputation: 3964

switch case using variables, repetitive code

I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:

 public void ButtonOnClick(View v) {

     switch (v.getId()) {
        case button1:
            if(button_list.get(1).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
            is_clicked= (String)button_list.get(1).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(1).setTag("1"); 
            }else{
                    button_list.get(1).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(1).setTag("0"); 
            }
            break;
        case button2:
            if(button_list.get(2).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
            is_clicked= (String)button_list.get(2).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(2).setTag("1"); 
            }else{
                    button_list.get(2).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(2).setTag("0"); 
            }
            break;
        case button3:
            if(button_list.get(3).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
            is_clicked= (String)button_list.get(3).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(3).setTag("1"); 
            }else{
                    button_list.get(3).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(3).setTag("0"); 
            }
            break;
        case button4:
            if(button_list.get(4).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
            is_clicked= (String)button_list.get(4).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(4).setTag("1"); 
            }else{
                    button_list.get(4).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(4).setTag("0"); 
            }
            break;
        case button5:
            if(button_list.get(5).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
            is_clicked= (String)button_list.get(5).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(5).setTag("1"); 
            }else{
                    button_list.get(5).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(5).setTag("0"); 
            }
            break;
        case button6:
            if(button_list.get(6).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
            is_clicked= (String)button_list.get(6).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(6).setTag("1"); 
            }else{
                    button_list.get(6).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(6).setTag("0"); 
            }
            break;
        case button7:
            if(button_list.get(7).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
            is_clicked= (String)button_list.get(7).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(7).setTag("1"); 
            }else{
                    button_list.get(7).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(7).setTag("0"); 
            }
            break;
        case button8:
            if(button_list.get(8).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
            is_clicked= (String)button_list.get(8).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(8).setTag("1"); 
            }else{
                    button_list.get(8).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(8).setTag("0"); 
            }
            break;
        case button9:
            if(button_list.get(9).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
            is_clicked= (String)button_list.get(9).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(9).setTag("1"); 
            }else{
                    button_list.get(9).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(9).setTag("0"); 
            }
            break;
            case button10:
                if(button_list.get(10).getTag().equals("0"))
                    enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
                is_clicked= (String)button_list.get(10).getTag();     
                if ("0".equals(is_clicked)){
                    button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
                    button_list.get(10).setTag("1");    
                }else{
                        button_list.get(10).setBackgroundResource(R.drawable.button_normal);
                        button_list.get(10).setTag("0");    
                }
                break;

Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:

public void ButtonOnClick(View v) {

    switch (v.getId()) {
        case button[i] 
            if(button_list.get(i).getTag().equals("0"))
                enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
            is_clicked= (String)button_list.get(i).getTag();      
            if ("0".equals(is_clicked)){
                button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
                button_list.get(i).setTag("1"); 
            }else{
                    button_list.get(i).setBackgroundResource(R.drawable.button_normal);
                    button_list.get(i).setTag("0"); 
            }
            break;

Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?

Upvotes: 0

Views: 225

Answers (2)

Vincent D.
Vincent D.

Reputation: 997

First you should never copy paste code, try to extract your code in another method.

If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o) So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :

public void ButtonOnClick(View v) {
    if(button_list.contains(v))
    {
            setButtonBackground(button_list.indexOf(v));
    }
}

private setButtonBackground(int buttonNumber)
{
    Button myButton = (Button) button_list.get(buttonNumber);

    if(myButton.getTag().equals("0"))
    {
        enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
    }

    is_clicked = (String) myButton.getTag();   

    if ("0".equals(is_clicked))
    {
        myButton.setBackgroundResource(R.drawable.button_pressed);
        myButton.setTag("1"); 
    }
    else
    {
        myButton.setBackgroundResource(R.drawable.button_normal);
        myButton.setTag("0"); 
    }
}

When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.

[...]
case button1:
        setButtonBackground(1);
        break;
case button2:
        setButtonBackground(2);
        break;
[...]

Let me know if it works for you

Upvotes: 1

Ted Hopp
Ted Hopp

Reputation: 234857

I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:

public void ButtonOnClick(View v) {
    Button btn = (Button) v;
    if (btn.getTag().equals("0")) {
        enter_txt.setText(enter_txt.getText()+btn.getText().toString());
    }
    is_clicked= (String) btn.getTag();
    if ("0".equals(is_clicked)){
        btn.setBackgroundResource(R.drawable.button_pressed);
        btn.setTag("1"); 
    }else{
        btn.setBackgroundResource(R.drawable.button_normal);
        btn.setTag("0"); 
    }
}

As an aside, you might consider using Boolean objects instead of String objects as the button tags.

Upvotes: 2

Related Questions