6

I have a few buttons on an activity for an app I am working on.

I have the text for each stored in an array (the data can change) and I am trying to update all of them with a for loop.

The Id's are button1, button2, and button3

This is what I want

for(int i=1; i<=splitter.length; i++){
        Button button = (Button)findViewById(R.id.button[i]);//<---How do i make this work

button.setText(spliter[i-1]);
    }
1
  • instead of saving ` button1, button2` to your string array, save your button1 id to int array Commented Sep 11, 2015 at 4:26

3 Answers 3

8

As a simple solution you should try iterating over the containing view's children:

Considering you have your buttons all inside a Layout like this:

<LinearLayout
    android:id="@+id/layout_container_buttons"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button1"/>

    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button2"/>

</LinearLayout>

Then just simple iterate over all the Layout children:

ViewGroup layout = (ViewGroup)findViewById(R.id.layout_container_buttons);
for (int i = 0; i < layout.getChildCount(); i++) {

    View child = layout.getChildAt(i);
    if(child instanceof Button)
    {
        Button button = (Button) child;
        button.setText(spliter[i]);
    }

}

A better approach, however, would be to create the buttons dynamically based on your array size and adding them to LinearLayout instead of copy/pasting them inside your layout.xml file. This would help you to have the exact number of buttons per each value on your array every time you may want to add/remove something to it.

ViewGroup layout = (ViewGroup) findViewById(R.id.layout_container_buttons);
for (int i = 0; i < splitter.length; i++) // iterate over your array
{
    // Create the button
    Button button = new Button(this);
    button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                                                         LinearLayout.LayoutParams.WRAP_CONTENT));
    button.setText(splitter[i]);
    layout.addView(button); // add to layout
}
Sign up to request clarification or add additional context in comments.

Comments

5
for (int i = 1; i <= splitter.length; i++) {
    Button button = (Button) findViewById(getResources().getIdentifier("button" + i, "id",
            this.getPackageName()));

    button.setText(spliter[i - 1]);
}

Hope it helps.

Comments

0

The instance names of the buttons are going to be the same. Like there will be multiple instances of the same name. How will you differentiate the buttons.

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.