Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create an n x 2 grid programmatically in Android Java code?
    primarykey
    data
    text
    <p>I have defined a custom view, as a Java class, and now I want to put them in a layout, programmatically.</p> <p>I can define a layout structure like this :</p> <pre><code>&lt;LinearLayout&gt; &lt;!-- vertical, main container --&gt; &lt;LinearLayout&gt; &lt;!-- 1st horizontal (consists of 2 columns (items) ) --&gt; &lt;CustomView /&gt; &lt;!-- 1 --&gt; &lt;CustomView /&gt; &lt;!-- 2 --&gt; &lt;/LinearLayout&gt; &lt;LinearLayout&gt; &lt;!-- 2nd horizontal (consists of 2 columns (items) ) --&gt; &lt;CustomView /&gt; &lt;!-- 1 --&gt; &lt;CustomView /&gt; &lt;!-- 2 --&gt; &lt;/LinearLayout&gt; &lt;!-- ..... --&gt; &lt;LinearLayout&gt; &lt;!-- nth horizontal (consists of n columns (items) ) --&gt; &lt;CustomView /&gt; &lt;!-- 1 --&gt; &lt;CustomView /&gt; &lt;!-- 2 --&gt; &lt;/LinearLayout&gt; &lt;/LinearLayout&gt;&lt;!-- vertical container --&gt; </code></pre> <p>but when I delete one of the custom views, it remains ugly. Suppose n=3 and I removed custom view number 4. I have :</p> <pre><code>1 2 3 5 6 </code></pre> <p>which does not look good, they should "fill the leading space" like this:</p> <pre><code>1 2 3 5 6 </code></pre> <p>How should I achieve such a thing?</p> <p>And please note that I construct the <code>CustomView</code>s in Java code (not in XML)</p> <p>I have tried getting the screen size, dividing it by n, and assign the value as the <code>LayoutParams.width</code> of the custom view.</p> <p>But getting the screen size in dp's is not a very accurate thing, according to some questions in StackOverflow.</p> <p>So how should I create a grid layout programmatically ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload