0

I have 9 (programmatically generated) buttons. I do this for all of them:

Button btnButton1 = new Button(this);
btnButton1.setText(getText(R.string.button_1));
btnButton1.setTextSize(BUTTON_TEXT_SIZE);
btnButton1.setHeight(BUTTON_HEIGHT);
btnButton1.setWidth(BUTTON_WIDTH);
btnButton1.setOnClickListener(buttonClicked(btnButton1));

(where buttonClicked is this:

  private final View.OnClickListener buttonClicked(final Button button)
  {
    return new View.OnClickListener()
    {
      public void onClick(View v)
      {
        tvButtons.append(button.getText().toString());
      }
    };        
  };

and tvButtons is a TextView. )

Is there a way I can do something like:

for (button : buttons)
{
   button.setText &c. &c.
}

to reduce the duplication in the code?

2 Answers 2

3

Firstly, I'd say that your onClick listener is overly complicated. I'd refactor it to look like this:

public void onClick(View v)
{
  tvButtons.append(((Button)v).getText().toString());
}

Then you can simply set the onclick listener of each button to the class that implements that method without having to supply the button object.

Then I'd do what Micah suggested and encapsulate the repeated portion of code in a method. Furthermore, you can create an array in strings.xml as follows:

<string-array name="buttonStrings">
    <item>String1</item>
    <item>String2</item>
    <item>String3</item>
</string-array>

Then you can retrieve that array using

getResources().getStringArray(R.array.buttonStrings);

and loop over that, calling the createButton method on each element. The whole thing should look something like this:

public class MyActivity extends Activity implements OnClickListener {

    public onCreate() {
        String[] strings = getResources().getStringArray(R.array.buttonStrings);

        for(int i=0;i<strings.length;++i) {
            createButton(strings[i]);
        }
    }

    private Button createButton(String s) {
        Button b = new Button(this);
        b.setText(s);
        b.setTextSize(BUTTON_TEXT_SIZE);
        b.setHeight(BUTTON_HEIGHT);
        b.setWidth(BUTTON_WIDTH);
        b.setOnClickListener(this);
        return b;
    }

    @Override
    public onClickListener(View v) {
        tvButtons.append(((Button)v).getText().toString());
    }
}

Hope that helps!

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

Comments

0

Simply make a method createButton(int textResourceId) that does all the work, and make nine calls to it with your text ids.

2 Comments

wouldn't you also have to pass in a reference to the button? So, createButton(Button btn, int textResourceId)
@dave.c no, the button would be created in the method itself. From Ben's code, he's in the activity, so that's all he would need.

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.