Create new button and add it in next position
<p>how can I create a button programmatically </p>

<pre><code>Button x = new Button (this);
</code></pre>

<p>but this button I add it in next available position. Now, if I do this and reach four created buttons, the fifth one will be outside the screen. How can I let the fifth one check if there is enough space to the right, if not, then the button would be created below the rest.</p>

<p>I tried grid layout and increase the column number but it is not good as I want to support Android 7.</p>