I want to create a layout dynamically in android which consist some buttons. But I am unable to find any way to dynamically create this layout.
Here is my layout :

I want to put buttons into next line after they completely filled in the width of device. Total number of buttons can be varied.
Is there any way to do this dynamically?
UPDATE :
As per your suggestions I’m using this code :
private void addQuestionPallete() {
int tempBtn = 30;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 0.1f);
LayoutParams param2 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f);
linearContent.setWeightSum(1f);
linearContent.setOrientation(LinearLayout.VERTICAL);
btnQuestionPalete = new Button[10];
for (int j = 0; j < tempBtn / 10; j++) {
linearQuestionPalettesRow = new LinearLayout(mContext);
linearQuestionPalettesRow.setOrientation(LinearLayout.HORIZONTAL);
linearQuestionPalettesRow.setLayoutParams(param2);
for (int i = 0; i < 10; i++) {
btnQuestionPalete[i] = new Button(mContext);
btnQuestionPalete[i].setLayoutParams(param);
btnQuestionPalete[i].setTextColor(Color.BLACK);
btnQuestionPalete[i].setText("5");
btnQuestionPalete[i].setId(fChexkBoxID++);
linearQuestionPalettesRow.addView(btnQuestionPalete[i]);
}
}
linearContent.addView(linearQuestionPalettesRow);
}
but its only showing single row of buttons. Where I’m doing wrong?
UPDATE :
OK I have solved my problem. I was adding layout out of for loop. thus the layout is adding only one time.
You can change the weight of the buttons as you like, making them larger or smaller and then adjust your counter to behave accordingly.
For e.g., if you decide to assign weight = 0.2 to each button, then your counter should count till 5 (1/0.2 = 5). You can also set the weight as 0.05 or 0.025 etc. depending upon the number of buttons you want in each row.
This way you need not worry about the different screen sizes because the weight property will adjust the views in any screen size.