You need at least n LinearLayout , where n is your number of lines. If you really want to use LinearLayout , then add your buttons using the following LayoutParams :
new LinearLayout.LayoutParams(0,LayoutParams.WRAP_CONTENT, 1/numButtonsInRow);
1/numButtonsInRow - button weight. For example, if you have 10 buttons in a row, each button will have a weight of 0.1 . If your layout is horizontal, set its width to 0, if it is set vertically, its height is 0.
In your case, you should probably use GridLayout or TableLayout .
source share