user493244
user493244

Reputation: 919

adding buttons and radiobuttons dynamically in android

In my project I want to add buttons and radio buttons dynamically. They should be placed side by side. For instance, if I had 10 buttons and 10 radiobuttons, I should get 2 buutons and 2 radiobuttons in each row. How can this be done?

Upvotes: 0

Views: 1408

Answers (1)

George
George

Reputation: 1327

for example if you want to add 7 buttons and radiobuttons to your LinearLayout you can use something like this

    private void createRadioButton() {
        final RadioButton[] radioButton = new RadioButton[7];
        RadioGroup radioGroup = new RadioGroup(this);
        radioGroup.setOrientation(RadioGroup.VERTICAL);
        for(int i = 0; i < 7; ++i) {
            radioButton[i]  = new RadioButton(this);
            Button buttonView = new Button(this);
            buttonView.setText("Button " + i);
            radioGroup.addView(radioButton[i]);
            radioGroup.addView(buttonView);
            radioButton[i].setText("Test" + i);
        }
        line.addView(radioGroup);
    }

Upvotes: 1

Related Questions